{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bayes Nets\n",
    "** A Julia package for Bayesian Networks **\n",
    "\n",
    "A Bayesian Network (BN) represents a probability distribution over a set of variables, $P(x_1, x_2, \\ldots, x_n)$. Bayesian networks leverage variable relations in order to efficiently decompose the joint distribution into smaller conditional probability distributions.\n",
    "\n",
    "A BN is defined by a directed acyclic graph and a set of conditional probability distributions. Each node in the graph corresponds to a variable $x_i$ and is associated with a conditional probability distribution $P(x_i \\mid \\text{parents}(x_i))$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Pkg.add(\"BayesNets\");`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Visualization of network structure is provided by the TikzGraphs package. Installation requirements (e.g., PGF/Tikz and pdf2svg) are provided [here](http://nbviewer.ipython.org/github/sisl/TikzGraphs.jl/blob/master/doc/TikzGraphs.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "srand(0) # seed the random number generator to 0, for a reproducible demonstration\n",
    "using BayesNets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Representation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bayesian Networks are represented with the `BayesNet` type. This type contains the directed acyclic graph (a LightTables.DiGraph) and a list of conditional probability distributions (a list of CPDs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we construct the BayesNet $a \\rightarrow b$, with Gaussians $a$ and $b$:\n",
    "\n",
    "$$\n",
    "a = \\mathcal{N}(0,1) \\qquad b = \\mathcal{N}(2a +3,1)\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"12.18pt\" height=\"40.786pt\" viewBox=\"0 0 12.18 40.786\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066000-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066000-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066000-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -0.00015625 -5.807063 L -0.00015625 -20.857844 \" transform=\"matrix(1,0,0,-1,6.09,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194299 1.593594 C -1.096643 0.995938 0.00101375 0.101406 0.297889 -0.00015625 C 0.00101375 -0.0978125 -1.096643 -0.99625 -1.194299 -1.593906 \" transform=\"matrix(0,1,1,0,6.09,26.46383)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066000-0-1\" x=\"3.599\" y=\"7.784\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066000-0-2\" x=\"3.321\" y=\"37.356\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CPD}({2, 1} directed simple Int64 graph, BayesNets.CPDs.CPD[BayesNets.CPDs.StaticCPD{Distributions.Normal{Float64}}(:a, Symbol[], Distributions.Normal{Float64}(μ=1.0, σ=1.0)), BayesNets.CPDs.LinearGaussianCPD(:b, Symbol[:a], [2.0], 3.0, 1.0)], Dict(:a=>1,:b=>2))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bn = BayesNet()\n",
    "push!(bn, StaticCPD(:a, Normal(1.0)))\n",
    "push!(bn, LinearGaussianCPD(:b, [:a], [2.0], 3.0, 1.0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conditional Probability Distributions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Conditional Probablity Distributions, $P(x_i \\mid \\text{parents}(x_i))$, are defined in BayesNets.CPDs. Each CPD knows its own name, the names of its parents, and is associated with a distribution from Distributions.jl."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| `CPDForm`                      | Description |\n",
    "| ------------------------------ | ----------- |\n",
    "| `StaticCPD`                    | Any `Distributions.distribution`; indepedent of any parents |\n",
    "| `FunctionalCPD`                | Allows for a CPD defined with a custom eval function |\n",
    "| `ParentFunctionalCPD`          | Modification to `FunctionalCPD` allowing the parent values to be passed in |\n",
    "| `CategoricalCPD`               | Categorical distribution, assumes integer parents in $1:N$ |\n",
    "| `LinearGaussianCPD`            | Linear Gaussian, assumes target and parents are numeric |\n",
    "| `ConditionalLinearGaussianCPD` | A linear Gaussian for each discrete parent instantiation|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each CPD can be learned from data using `fit`.\n",
    "\n",
    "Here we learn the same network as above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"12.18pt\" height=\"40.786pt\" viewBox=\"0 0 12.18 40.786\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066001-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066001-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066001-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -0.00015625 -5.807063 L -0.00015625 -20.857844 \" transform=\"matrix(1,0,0,-1,6.09,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194299 1.593594 C -1.096643 0.995938 0.00101375 0.101406 0.297889 -0.00015625 C 0.00101375 -0.0978125 -1.096643 -0.99625 -1.194299 -1.593906 \" transform=\"matrix(0,1,1,0,6.09,26.46383)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066001-0-1\" x=\"3.599\" y=\"7.784\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066001-0-2\" x=\"3.321\" y=\"37.356\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CPD}({2, 1} directed simple Int64 graph, BayesNets.CPDs.CPD[BayesNets.CPDs.StaticCPD{Distributions.Normal{Float64}}(:a, Symbol[], Distributions.Normal{Float64}(μ=-0.0248205, σ=1.051)), BayesNets.CPDs.LinearGaussianCPD(:b, Symbol[:a], [1.86726], 2.84991, 2.23611)], Dict(:a=>1,:b=>2))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = randn(100)\n",
    "b = randn(100) .+ 2*a .+ 3\n",
    "\n",
    "data = DataFrame(a=a, b=b)\n",
    "cpdA = fit(StaticCPD{Normal}, data, :a)\n",
    "cpdB = fit(LinearGaussianCPD, data, :b, [:a])\n",
    "\n",
    "bn2 = BayesNet([cpdA, cpdB])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each `CPD` implements four functions:\n",
    "\n",
    "* `name(cpd)` - obtain the name of the variable target variable\n",
    "* `parents(cpd)` - obtain the list of parents\n",
    "* `nparams(cpd` - obtain the number of free parameters in the CPD\n",
    "* `cpd(assignment)` - allows calling `cpd()` to obtain the conditional distribution\n",
    "* `Distributions.fit(Type{CPD}, data, target, parents)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Distributions.Normal{Float64}(μ=3.7835395874388134, σ=2.236109637835203)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cpdB(:a=>0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Several functions conveniently condition and then produce their return values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rand(cpdB, :a=>0.5) # condition and then sample\n",
    "pdf(cpdB, :a=>1.0, :b=>3.0) # condition and then compute pdf(distribution, 3)\n",
    "logpdf(cpdB, :a=>1.0, :b=>3.0) # condition and then compute logpdf(distribution, 3);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The NamedCategorical distribution allows for String or Symbol return values.\n",
    "The FunctionalCPD allows for crafting quick and simple CPDs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"37.695pt\" height=\"43.949pt\" viewBox=\"0 0 37.695 43.949\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.78125 -1.40625 C 3.78125 -1.9375 3.421875 -2.328125 3.3125 -2.453125 C 2.984375 -2.765625 2.453125 -2.890625 2.03125 -2.96875 C 1.46875 -3.078125 1 -3.078125 1 -3.65625 C 1 -4 1.0625 -4.28125 1.921875 -4.28125 C 3.015625 -4.28125 2.875 -3.515625 2.890625 -3.203125 C 2.90625 -3.109375 3.34375 -3.015625 3.34375 -3.015625 L 3.203125 -2.984375 C 3.34375 -2.984375 3.53125 -3.15625 3.53125 -3.34375 L 3.53125 -4.359375 C 3.53125 -4.53125 3.34375 -4.734375 3.234375 -4.734375 C 3.1875 -4.734375 3.015625 -4.6875 2.890625 -4.578125 C 2.859375 -4.53125 2.765625 -4.453125 2.84375 -4.515625 C 2.625 -4.6875 2.078125 -4.734375 1.921875 -4.734375 C 0.703125 -4.734375 0.125 -3.921875 0.125 -3.375 C 0.125 -3.015625 0.34375 -2.65625 0.625 -2.4375 C 0.9375 -2.171875 1.359375 -2.078125 2.078125 -1.9375 C 2.296875 -1.890625 2.90625 -1.859375 2.90625 -1.140625 C 2.90625 -0.640625 2.765625 -0.375 1.984375 -0.375 C 1.140625 -0.375 0.984375 -0.8125 0.796875 -1.65625 C 0.765625 -1.78125 0.5625 -1.96875 0.453125 -1.96875 C 0.328125 -1.96875 0.125 -1.75 0.125 -1.578125 L 0.125 -0.265625 C 0.125 -0.09375 0.328125 0.109375 0.4375 0.109375 C 0.484375 0.109375 0.640625 0.0625 0.828125 -0.125 C 0.84375 -0.15625 0.84375 -0.171875 0.890625 -0.21875 C 1.1875 0.0625 1.78125 0.109375 1.984375 0.109375 C 3.125 0.109375 3.78125 -0.6875 3.78125 -1.40625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 2.65625 -0.125 L 2.65625 -0.578125 C 1.796875 -0.578125 1.96875 -0.484375 1.96875 -0.875 L 1.96875 -4.6875 L 0.171875 -4.546875 L 0.171875 -3.984375 C 1.015625 -3.984375 0.90625 -4.0625 0.90625 -3.5625 L 0.90625 -0.890625 C 0.90625 -0.4375 1 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.421875 -0.03125 C 1.78125 -0.03125 2.125 -0.015625 2.65625 0.015625 Z M 2.109375 -6.15625 C 2.109375 -6.421875 1.6875 -6.8125 1.390625 -6.8125 C 1.046875 -6.8125 0.640625 -6.40625 0.640625 -6.15625 C 0.640625 -5.875 1.078125 -5.5 1.375 -5.5 C 1.71875 -5.5 2.109375 -5.90625 2.109375 -6.15625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.03125 -4.15625 C 5.03125 -4.328125 4.71875 -4.78125 4.328125 -4.78125 C 4.125 -4.78125 3.546875 -4.6875 3.25 -4.390625 C 2.984375 -4.609375 2.4375 -4.671875 2.21875 -4.671875 C 1.28125 -4.671875 0.40625 -3.84375 0.40625 -3.078125 C 0.40625 -2.640625 0.671875 -2.171875 0.828125 -2.046875 C 0.796875 -2 0.5625 -1.578125 0.5625 -1.21875 C 0.5625 -0.921875 0.75 -0.453125 0.84375 -0.375 C 0.59375 -0.3125 0.078125 0.265625 0.078125 0.65625 C 0.078125 1.375 1.265625 2.046875 2.484375 2.046875 C 3.65625 2.046875 4.890625 1.421875 4.890625 0.640625 C 4.890625 0.296875 4.6875 -0.3125 4.1875 -0.59375 C 3.65625 -0.875 2.9375 -0.921875 2.328125 -0.921875 C 2.078125 -0.921875 1.65625 -0.921875 1.578125 -0.921875 C 1.265625 -0.96875 1.25 -1.140625 1.25 -1.453125 C 1.25 -1.5 1.203125 -1.640625 1.25 -1.703125 C 1.46875 -1.5625 2.03125 -1.484375 2.21875 -1.484375 C 3.140625 -1.484375 4.03125 -2.296875 4.03125 -3.078125 C 4.03125 -3.4375 3.8125 -3.90625 3.65625 -4.03125 C 3.921875 -4.28125 4.140625 -4.296875 4.3125 -4.296875 L 4.453125 -4.34375 C 4.453125 -4.34375 4.390625 -4.296875 4.015625 -4.421875 C 4.3125 -4.515625 4.0625 -4.265625 4.0625 -4.140625 C 4.0625 -3.984375 4.390625 -3.734375 4.546875 -3.734375 C 4.640625 -3.734375 5.03125 -3.921875 5.03125 -4.15625 Z M 2.875 -3.078125 C 2.875 -2.8125 2.9375 -2.59375 2.78125 -2.34375 C 2.703125 -2.21875 2.609375 -1.984375 2.21875 -1.984375 C 1.34375 -1.984375 1.546875 -2.84375 1.546875 -3.078125 C 1.546875 -3.34375 1.5 -3.5625 1.640625 -3.8125 C 1.71875 -3.9375 1.8125 -4.171875 2.21875 -4.171875 C 3.078125 -4.171875 2.875 -3.3125 2.875 -3.078125 Z M 3.984375 0.65625 C 3.984375 1.203125 3.46875 1.5625 2.5 1.5625 C 1.484375 1.5625 1 1.1875 1 0.65625 C 1 0.203125 1.171875 -0.046875 1.609375 -0.0625 L 2.203125 -0.0625 C 3.0625 -0.0625 3.984375 -0.203125 3.984375 0.65625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.53125 -0.125 L 5.53125 -0.578125 C 4.8125 -0.578125 4.765625 -0.4375 4.75 -0.734375 L 4.75 -2.640625 C 4.75 -3.5 4.6875 -3.90625 4.390625 -4.265625 C 4.25 -4.4375 3.78125 -4.671875 3.203125 -4.671875 C 2.359375 -4.671875 1.71875 -4 1.59375 -3.71875 L 1.953125 -3.71875 L 1.953125 -7.203125 L 0.125 -7.0625 L 0.125 -6.5 C 1.015625 -6.5 0.890625 -6.5625 0.890625 -6.078125 L 0.890625 -0.890625 C 0.890625 -0.4375 0.984375 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.453125 -0.03125 L 2.765625 0.015625 L 2.765625 -0.578125 C 1.890625 -0.578125 1.984375 -0.4375 1.984375 -0.890625 L 1.984375 -2.71875 C 1.984375 -3.765625 2.5 -4.1875 3.125 -4.1875 C 3.765625 -4.1875 3.671875 -3.78125 3.671875 -3.203125 L 3.671875 -0.890625 C 3.671875 -0.4375 3.765625 -0.578125 2.890625 -0.578125 L 2.890625 0.015625 L 4.21875 -0.03125 L 5.53125 0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.515625 -1.359375 L 3.515625 -2.078125 L 2.859375 -2.078125 L 2.859375 -1.390625 C 2.859375 -0.640625 2.765625 -0.40625 2.390625 -0.40625 C 1.71875 -0.40625 1.921875 -1.171875 1.921875 -1.34375 L 1.921875 -3.984375 L 3.34375 -3.984375 L 3.34375 -4.5625 L 1.921875 -4.5625 L 1.921875 -6.40625 L 1.28125 -6.40625 C 1.265625 -5.515625 1.171875 -4.515625 -0.015625 -4.46875 L -0.015625 -3.984375 L 0.84375 -3.984375 L 0.84375 -1.359375 C 0.84375 -0.140625 1.96875 0.109375 2.328125 0.109375 C 3.03125 0.109375 3.515625 -0.734375 3.515625 -1.359375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-6\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.0625 -1.578125 4 -1.578125 C 3.921875 -1.578125 3.703125 -1.453125 3.671875 -1.359375 C 3.328125 -0.34375 2.625 -0.40625 2.53125 -0.40625 C 2.03125 -0.40625 1.78125 -0.671875 1.546875 -1.03125 C 1.25 -1.515625 1.3125 -2.078125 1.3125 -2.296875 L 3.890625 -2.296875 C 4.109375 -2.296875 4.34375 -2.4375 4.34375 -2.640625 C 4.34375 -3.625 3.59375 -4.734375 2.359375 -4.734375 C 1.203125 -4.734375 0.078125 -3.5625 0.078125 -2.328125 C 0.078125 -0.984375 1.328125 0.109375 2.46875 0.109375 C 3.6875 0.109375 4.34375 -1.125 4.34375 -1.3125 Z M 3.484375 -2.78125 L 1.3125 -2.78125 C 1.375 -4.125 2.015625 -4.25 2.359375 -4.25 C 3.375 -4.25 3.28125 -3.03125 3.28125 -2.78125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-7\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.453125 -0.125 L 5.453125 -0.578125 C 4.5625 -0.578125 4.671875 -0.515625 4.671875 -1 L 4.671875 -7.203125 L 2.84375 -7.0625 L 2.84375 -6.5 C 3.734375 -6.5 3.625 -6.5625 3.625 -6.078125 L 3.625 -3.921875 L 3.953125 -4.015625 C 3.671875 -4.375 3.09375 -4.671875 2.5625 -4.671875 C 1.390625 -4.671875 0.140625 -3.5625 0.140625 -2.265625 C 0.140625 -1 1.3125 0.109375 2.453125 0.109375 C 3.09375 0.109375 3.671875 -0.265625 3.921875 -0.59375 L 3.59375 -0.671875 L 3.59375 0.125 L 5.453125 -0.015625 Z M 3.59375 -1.3125 C 3.59375 -1.125 3.65625 -1.203125 3.53125 -1.03125 C 3.234375 -0.5625 2.9375 -0.375 2.5 -0.375 C 2.046875 -0.375 1.828125 -0.59375 1.59375 -0.984375 C 1.328125 -1.390625 1.359375 -1.859375 1.359375 -2.265625 C 1.359375 -2.625 1.328125 -3.140625 1.609375 -3.59375 C 1.828125 -3.890625 2.0625 -4.1875 2.609375 -4.1875 C 2.953125 -4.1875 3.234375 -4.078125 3.53125 -3.625 C 3.65625 -3.453125 3.59375 -3.53125 3.59375 -3.34375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-8\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-9\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.234375 -4.671875 3.109375 -4.671875 C 2.328125 -4.671875 1.78125 -4.203125 1.578125 -3.984375 L 1.90625 -3.875 L 1.90625 -4.6875 L 0.078125 -4.546875 L 0.078125 -3.984375 C 0.984375 -3.984375 0.859375 -4.0625 0.859375 -3.625 L 0.859375 1.046875 C 0.859375 1.5 0.953125 1.359375 0.078125 1.359375 L 0.078125 1.9375 L 1.390625 1.90625 L 2.71875 1.9375 L 2.71875 1.359375 C 1.859375 1.359375 1.9375 1.5 1.9375 1.046875 L 1.9375 -0.71875 L 1.546875 -0.71875 C 1.59375 -0.5625 2.21875 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.28125 C 4.171875 -1.078125 3.671875 -0.375 2.9375 -0.375 C 2.53125 -0.375 2.296875 -0.53125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.0625 1.9375 -1.265625 L 1.9375 -3.5 C 2.171875 -3.90625 2.515625 -4.15625 3.03125 -4.15625 C 3.765625 -4.15625 4.171875 -3.40625 4.171875 -2.28125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066002-0-10\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.265625 -4.109375 L 5.265625 -4.578125 C 4.828125 -4.546875 4.546875 -4.53125 4.3125 -4.53125 L 3.25 -4.578125 L 3.25 -3.984375 C 3.765625 -3.984375 3.71875 -3.9375 3.71875 -3.6875 C 3.71875 -3.59375 3.703125 -3.640625 3.65625 -3.515625 L 2.65625 -1.0625 L 3.046875 -1 L 1.9375 -3.671875 C 1.90625 -3.78125 1.890625 -3.8125 1.890625 -3.859375 C 1.890625 -4.109375 2.046875 -3.984375 2.4375 -3.984375 L 2.4375 -4.578125 L 1.15625 -4.53125 C 0.890625 -4.53125 0.484375 -4.546875 -0.015625 -4.578125 L -0.015625 -3.984375 C 0.671875 -3.984375 0.65625 -4.109375 0.796875 -3.765625 L 2.265625 -0.15625 L 2.046875 0.390625 C 1.828125 0.9375 1.75 1.5625 1.109375 1.5625 C 1.0625 1.5625 0.96875 1.59375 0.984375 1.609375 C 0.953125 1.609375 1.21875 1.25 1.21875 1.09375 C 1.21875 0.84375 0.84375 0.53125 0.609375 0.53125 C 0.40625 0.53125 -0.015625 0.8125 -0.015625 1.109375 C -0.015625 1.5625 0.609375 2.046875 1.109375 2.046875 C 1.734375 2.046875 2.34375 1.34375 2.578125 0.78125 L 4.34375 -3.484375 C 4.59375 -4.109375 4.90625 -3.984375 5.265625 -3.984375 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 0.00065625 -8.004594 L 0.00065625 -19.891313 \" transform=\"matrix(1,0,0,-1,18.847,7.804)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.19444 1.594406 C -1.096784 0.99675 0.0008725 0.0983125 0.297748 0.00065625 C 0.0008725 -0.100906 -1.096784 -0.995438 -1.19444 -1.593094 \" transform=\"matrix(0,1,1,0,18.847,27.69444)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-1\" x=\"3.321\" y=\"10.235\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-2\" x=\"7.24628\" y=\"10.235\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-3\" x=\"10.015894\" y=\"10.235\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-4\" x=\"14.997214\" y=\"10.235\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-5\" x=\"20.536442\" y=\"10.235\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-6\" x=\"24.411909\" y=\"10.235\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-7\" x=\"28.835321\" y=\"10.235\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-4\" x=\"5.418\" y=\"38.586\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-8\" x=\"10.957228\" y=\"38.586\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-9\" x=\"15.938548\" y=\"38.586\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-9\" x=\"21.477776\" y=\"38.586\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066002-0-10\" x=\"27.017004\" y=\"38.586\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CPD}({2, 1} directed simple Int64 graph, BayesNets.CPDs.CPD[BayesNets.CPDs.StaticCPD{BayesNets.CPDs.NamedCategorical{Symbol}}(:sighted, Symbol[], NamedCategorical with entries:\n",
       "\t  0.4000:  bird\n",
       "\t  0.0500:  superman\n",
       "\t  0.5500:  plane\n",
       "), BayesNets.CPDs.FunctionalCPD{Distributions.Bernoulli}(:happy, Symbol[:sighted], #3)], Dict(:sighted=>1,:happy=>2))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bn2 = BayesNet()\n",
    "push!(bn2, StaticCPD(:sighted, NamedCategorical([:bird, :plane, :superman], [0.40, 0.55, 0.05])))\n",
    "push!(bn2, FunctionalCPD{Bernoulli}(:happy, [:sighted], a->Bernoulli(a == :superman ? 0.95 : 0.2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Variables can be removed by name using `delete!`. A warning will be issued when removing a CPD with children."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"37.695pt\" height=\"15.607pt\" viewBox=\"0 0 37.695 15.607\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066003-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066003-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.78125 -1.40625 C 3.78125 -1.9375 3.421875 -2.328125 3.3125 -2.453125 C 2.984375 -2.765625 2.453125 -2.890625 2.03125 -2.96875 C 1.46875 -3.078125 1 -3.078125 1 -3.65625 C 1 -4 1.0625 -4.28125 1.921875 -4.28125 C 3.015625 -4.28125 2.875 -3.515625 2.890625 -3.203125 C 2.90625 -3.109375 3.34375 -3.015625 3.34375 -3.015625 L 3.203125 -2.984375 C 3.34375 -2.984375 3.53125 -3.15625 3.53125 -3.34375 L 3.53125 -4.359375 C 3.53125 -4.53125 3.34375 -4.734375 3.234375 -4.734375 C 3.1875 -4.734375 3.015625 -4.6875 2.890625 -4.578125 C 2.859375 -4.53125 2.765625 -4.453125 2.84375 -4.515625 C 2.625 -4.6875 2.078125 -4.734375 1.921875 -4.734375 C 0.703125 -4.734375 0.125 -3.921875 0.125 -3.375 C 0.125 -3.015625 0.34375 -2.65625 0.625 -2.4375 C 0.9375 -2.171875 1.359375 -2.078125 2.078125 -1.9375 C 2.296875 -1.890625 2.90625 -1.859375 2.90625 -1.140625 C 2.90625 -0.640625 2.765625 -0.375 1.984375 -0.375 C 1.140625 -0.375 0.984375 -0.8125 0.796875 -1.65625 C 0.765625 -1.78125 0.5625 -1.96875 0.453125 -1.96875 C 0.328125 -1.96875 0.125 -1.75 0.125 -1.578125 L 0.125 -0.265625 C 0.125 -0.09375 0.328125 0.109375 0.4375 0.109375 C 0.484375 0.109375 0.640625 0.0625 0.828125 -0.125 C 0.84375 -0.15625 0.84375 -0.171875 0.890625 -0.21875 C 1.1875 0.0625 1.78125 0.109375 1.984375 0.109375 C 3.125 0.109375 3.78125 -0.6875 3.78125 -1.40625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066003-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 2.65625 -0.125 L 2.65625 -0.578125 C 1.796875 -0.578125 1.96875 -0.484375 1.96875 -0.875 L 1.96875 -4.6875 L 0.171875 -4.546875 L 0.171875 -3.984375 C 1.015625 -3.984375 0.90625 -4.0625 0.90625 -3.5625 L 0.90625 -0.890625 C 0.90625 -0.4375 1 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.421875 -0.03125 C 1.78125 -0.03125 2.125 -0.015625 2.65625 0.015625 Z M 2.109375 -6.15625 C 2.109375 -6.421875 1.6875 -6.8125 1.390625 -6.8125 C 1.046875 -6.8125 0.640625 -6.40625 0.640625 -6.15625 C 0.640625 -5.875 1.078125 -5.5 1.375 -5.5 C 1.71875 -5.5 2.109375 -5.90625 2.109375 -6.15625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066003-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.03125 -4.15625 C 5.03125 -4.328125 4.71875 -4.78125 4.328125 -4.78125 C 4.125 -4.78125 3.546875 -4.6875 3.25 -4.390625 C 2.984375 -4.609375 2.4375 -4.671875 2.21875 -4.671875 C 1.28125 -4.671875 0.40625 -3.84375 0.40625 -3.078125 C 0.40625 -2.640625 0.671875 -2.171875 0.828125 -2.046875 C 0.796875 -2 0.5625 -1.578125 0.5625 -1.21875 C 0.5625 -0.921875 0.75 -0.453125 0.84375 -0.375 C 0.59375 -0.3125 0.078125 0.265625 0.078125 0.65625 C 0.078125 1.375 1.265625 2.046875 2.484375 2.046875 C 3.65625 2.046875 4.890625 1.421875 4.890625 0.640625 C 4.890625 0.296875 4.6875 -0.3125 4.1875 -0.59375 C 3.65625 -0.875 2.9375 -0.921875 2.328125 -0.921875 C 2.078125 -0.921875 1.65625 -0.921875 1.578125 -0.921875 C 1.265625 -0.96875 1.25 -1.140625 1.25 -1.453125 C 1.25 -1.5 1.203125 -1.640625 1.25 -1.703125 C 1.46875 -1.5625 2.03125 -1.484375 2.21875 -1.484375 C 3.140625 -1.484375 4.03125 -2.296875 4.03125 -3.078125 C 4.03125 -3.4375 3.8125 -3.90625 3.65625 -4.03125 C 3.921875 -4.28125 4.140625 -4.296875 4.3125 -4.296875 L 4.453125 -4.34375 C 4.453125 -4.34375 4.390625 -4.296875 4.015625 -4.421875 C 4.3125 -4.515625 4.0625 -4.265625 4.0625 -4.140625 C 4.0625 -3.984375 4.390625 -3.734375 4.546875 -3.734375 C 4.640625 -3.734375 5.03125 -3.921875 5.03125 -4.15625 Z M 2.875 -3.078125 C 2.875 -2.8125 2.9375 -2.59375 2.78125 -2.34375 C 2.703125 -2.21875 2.609375 -1.984375 2.21875 -1.984375 C 1.34375 -1.984375 1.546875 -2.84375 1.546875 -3.078125 C 1.546875 -3.34375 1.5 -3.5625 1.640625 -3.8125 C 1.71875 -3.9375 1.8125 -4.171875 2.21875 -4.171875 C 3.078125 -4.171875 2.875 -3.3125 2.875 -3.078125 Z M 3.984375 0.65625 C 3.984375 1.203125 3.46875 1.5625 2.5 1.5625 C 1.484375 1.5625 1 1.1875 1 0.65625 C 1 0.203125 1.171875 -0.046875 1.609375 -0.0625 L 2.203125 -0.0625 C 3.0625 -0.0625 3.984375 -0.203125 3.984375 0.65625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066003-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.53125 -0.125 L 5.53125 -0.578125 C 4.8125 -0.578125 4.765625 -0.4375 4.75 -0.734375 L 4.75 -2.640625 C 4.75 -3.5 4.6875 -3.90625 4.390625 -4.265625 C 4.25 -4.4375 3.78125 -4.671875 3.203125 -4.671875 C 2.359375 -4.671875 1.71875 -4 1.59375 -3.71875 L 1.953125 -3.71875 L 1.953125 -7.203125 L 0.125 -7.0625 L 0.125 -6.5 C 1.015625 -6.5 0.890625 -6.5625 0.890625 -6.078125 L 0.890625 -0.890625 C 0.890625 -0.4375 0.984375 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.453125 -0.03125 L 2.765625 0.015625 L 2.765625 -0.578125 C 1.890625 -0.578125 1.984375 -0.4375 1.984375 -0.890625 L 1.984375 -2.71875 C 1.984375 -3.765625 2.5 -4.1875 3.125 -4.1875 C 3.765625 -4.1875 3.671875 -3.78125 3.671875 -3.203125 L 3.671875 -0.890625 C 3.671875 -0.4375 3.765625 -0.578125 2.890625 -0.578125 L 2.890625 0.015625 L 4.21875 -0.03125 L 5.53125 0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066003-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.515625 -1.359375 L 3.515625 -2.078125 L 2.859375 -2.078125 L 2.859375 -1.390625 C 2.859375 -0.640625 2.765625 -0.40625 2.390625 -0.40625 C 1.71875 -0.40625 1.921875 -1.171875 1.921875 -1.34375 L 1.921875 -3.984375 L 3.34375 -3.984375 L 3.34375 -4.5625 L 1.921875 -4.5625 L 1.921875 -6.40625 L 1.28125 -6.40625 C 1.265625 -5.515625 1.171875 -4.515625 -0.015625 -4.46875 L -0.015625 -3.984375 L 0.84375 -3.984375 L 0.84375 -1.359375 C 0.84375 -0.140625 1.96875 0.109375 2.328125 0.109375 C 3.03125 0.109375 3.515625 -0.734375 3.515625 -1.359375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066003-0-6\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.0625 -1.578125 4 -1.578125 C 3.921875 -1.578125 3.703125 -1.453125 3.671875 -1.359375 C 3.328125 -0.34375 2.625 -0.40625 2.53125 -0.40625 C 2.03125 -0.40625 1.78125 -0.671875 1.546875 -1.03125 C 1.25 -1.515625 1.3125 -2.078125 1.3125 -2.296875 L 3.890625 -2.296875 C 4.109375 -2.296875 4.34375 -2.4375 4.34375 -2.640625 C 4.34375 -3.625 3.59375 -4.734375 2.359375 -4.734375 C 1.203125 -4.734375 0.078125 -3.5625 0.078125 -2.328125 C 0.078125 -0.984375 1.328125 0.109375 2.46875 0.109375 C 3.6875 0.109375 4.34375 -1.125 4.34375 -1.3125 Z M 3.484375 -2.78125 L 1.3125 -2.78125 C 1.375 -4.125 2.015625 -4.25 2.359375 -4.25 C 3.375 -4.25 3.28125 -3.03125 3.28125 -2.78125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066003-0-7\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.453125 -0.125 L 5.453125 -0.578125 C 4.5625 -0.578125 4.671875 -0.515625 4.671875 -1 L 4.671875 -7.203125 L 2.84375 -7.0625 L 2.84375 -6.5 C 3.734375 -6.5 3.625 -6.5625 3.625 -6.078125 L 3.625 -3.921875 L 3.953125 -4.015625 C 3.671875 -4.375 3.09375 -4.671875 2.5625 -4.671875 C 1.390625 -4.671875 0.140625 -3.5625 0.140625 -2.265625 C 0.140625 -1 1.3125 0.109375 2.453125 0.109375 C 3.09375 0.109375 3.671875 -0.265625 3.921875 -0.59375 L 3.59375 -0.671875 L 3.59375 0.125 L 5.453125 -0.015625 Z M 3.59375 -1.3125 C 3.59375 -1.125 3.65625 -1.203125 3.53125 -1.03125 C 3.234375 -0.5625 2.9375 -0.375 2.5 -0.375 C 2.046875 -0.375 1.828125 -0.59375 1.59375 -0.984375 C 1.328125 -1.390625 1.359375 -1.859375 1.359375 -2.265625 C 1.359375 -2.625 1.328125 -3.140625 1.609375 -3.59375 C 1.828125 -3.890625 2.0625 -4.1875 2.609375 -4.1875 C 2.953125 -4.1875 3.234375 -4.078125 3.53125 -3.625 C 3.65625 -3.453125 3.59375 -3.53125 3.59375 -3.34375 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066003-0-1\" x=\"3.321\" y=\"10.234\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066003-0-2\" x=\"7.24628\" y=\"10.234\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066003-0-3\" x=\"10.015894\" y=\"10.234\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066003-0-4\" x=\"14.997214\" y=\"10.234\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066003-0-5\" x=\"20.536442\" y=\"10.234\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066003-0-6\" x=\"24.411909\" y=\"10.234\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066003-0-7\" x=\"28.835321\" y=\"10.234\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CPD}({1, 0} directed simple Int64 graph, BayesNets.CPDs.CPD[BayesNets.CPDs.StaticCPD{BayesNets.CPDs.NamedCategorical{Symbol}}(:sighted, Symbol[], NamedCategorical with entries:\n",
       "\t  0.4000:  bird\n",
       "\t  0.0500:  superman\n",
       "\t  0.5500:  plane\n",
       ")], Dict(:sighted=>1))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delete!(bn2, :happy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Likelihood \n",
    "\n",
    "A Bayesian Network represents a joint probability distribution, $P(x_1, x_2, \\ldots, x_n)$. \n",
    "Assignments are represented as dictionaries mapping variable names (Symbols) to variable values.\n",
    "We can evaluate probabilities as we would with Distributions.jl, only we use exclamation points as we modify the internal state when we condition:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.01900834726778591"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pdf(bn, :a=>0.5, :b=>2.0) # evaluate the probability density"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also evaluate the likelihood of a dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = DataFrame(a=[0.5,1.0,2.0], b=[4.0,5.0,7.0])\n",
    "pdf(bn, data)    #  0.00215\n",
    "logpdf(bn, data) # -6.1386;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or the likelihood for a particular cpd:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-5.200981176187056"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pdf(cpdB, data)    #  0.006\n",
    "logpdf(cpdB, data) # -5.201"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sampling\n",
    "\n",
    "Assignments can be sampled from a `BayesNet`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dict{Symbol,Any} with 2 entries:\n",
       "  :a => 1.06468\n",
       "  :b => 6.03699"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand(bn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th></tr></thead><tbody><tr><th>1</th><td>0.7623039965961161</td><td>3.4917223005147227</td></tr><tr><th>2</th><td>-0.8005501733900879</td><td>0.23443764725795835</td></tr><tr><th>3</th><td>2.2115810954933637</td><td>7.515865521050183</td></tr><tr><th>4</th><td>-0.27359849027000194</td><td>3.7133964333979224</td></tr><tr><th>5</th><td>-0.42515634692622406</td><td>2.4710294668620785</td></tr></tbody></table>"
      ],
      "text/plain": [
       "5×2 DataFrames.DataFrame\n",
       "│ Row │ a         │ b        │\n",
       "├─────┼───────────┼──────────┤\n",
       "│ 1   │ 0.762304  │ 3.49172  │\n",
       "│ 2   │ -0.80055  │ 0.234438 │\n",
       "│ 3   │ 2.21158   │ 7.51587  │\n",
       "│ 4   │ -0.273598 │ 3.7134   │\n",
       "│ 5   │ -0.425156 │ 2.47103  │"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand(bn, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In general, sampling can be done according to `rand(BayesNet, BayesNetSampler, nsamples)` to produce a table of samples, `rand(BayesNet, BayesNetSampler)` to produce a single Assignment, or `rand!(Assignment, BayesNet, BayesNetSampler)` to modify an assignment in-place.\n",
    "\n",
    "New samplers need only implement `rand!`.\n",
    "\n",
    "The functions above default to the `DirectSampler`, which samples the variables in topographical order."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rejection sampling can be used to draw samples that are consistent with a provided assignment:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"40.248pt\" height=\"40.786pt\" viewBox=\"0 0 40.248 40.786\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066004-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066004-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066004-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066004-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.03125 -1.5625 4 -1.5625 C 3.921875 -1.5625 3.703125 -1.453125 3.671875 -1.390625 C 3.390625 -0.453125 2.9375 -0.40625 2.578125 -0.40625 C 2.046875 -0.40625 1.359375 -0.703125 1.359375 -2.296875 C 1.359375 -3.921875 1.984375 -4.21875 2.515625 -4.21875 C 2.609375 -4.21875 3.09375 -4.25 3.234375 -4.09375 C 3.171875 -4.09375 2.90625 -3.65625 2.90625 -3.515625 C 2.90625 -3.265625 3.296875 -2.9375 3.5625 -2.9375 C 3.828125 -2.9375 4.234375 -3.234375 4.234375 -3.53125 C 4.234375 -4.203125 3.265625 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.140625 -3.515625 0.140625 -2.28125 C 0.140625 -1 1.328125 0.109375 2.484375 0.109375 C 3.8125 0.109375 4.34375 -1.21875 4.34375 -1.3125 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 2.903844 -5.807625 L 11.064 -22.127937 \" transform=\"matrix(1,0,0,-1,5.811,6.833)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196486 1.593872 C -1.095579 0.995324 0.000581384 0.0992235 0.298113 -0.00169962 C 0.000577819 -0.100872 -1.095614 -0.996933 -1.194803 -1.595478 \" transform=\"matrix(0.44902,0.898,0.898,-0.44902,16.87548,28.962)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 24.829625 -7.030281 L 17.28275 -22.127937 \" transform=\"matrix(1,0,0,-1,5.811,6.833)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194772 1.595487 C -1.095593 0.996935 0.000592984 0.100846 0.298129 0.00166759 C 0.000592984 -0.0992511 -1.095593 -0.99534 -1.196512 -1.593892 \" transform=\"matrix(-0.449,0.898,0.898,0.449,23.0933,28.962)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066004-0-1\" x=\"3.321\" y=\"9.009\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066004-0-2\" x=\"31.388\" y=\"10.235\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066004-0-3\" x=\"17.773\" y=\"37.356\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CPD}({3, 2} directed simple Int64 graph, BayesNets.CPDs.CPD[BayesNets.CPDs.StaticCPD{Distributions.Categorical{Float64}}(:a, Symbol[], Distributions.Categorical{Float64}(K=2, p=[0.3, 0.7])), BayesNets.CPDs.StaticCPD{Distributions.Categorical{Float64}}(:b, Symbol[], Distributions.Categorical{Float64}(K=2, p=[0.6, 0.4])), BayesNets.CPDs.CategoricalCPD{Distributions.Bernoulli}(:c, Symbol[:a, :b], [2, 2], Distributions.Bernoulli[Distributions.Bernoulli{Float64}(p=0.1), Distributions.Bernoulli{Float64}(p=0.2), Distributions.Bernoulli{Float64}(p=1.0), Distributions.Bernoulli{Float64}(p=0.4)])], Dict(:a=>1,:b=>2,:c=>3))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bn = BayesNet()\n",
    "push!(bn, StaticCPD(:a, Categorical([0.3,0.7])))\n",
    "push!(bn, StaticCPD(:b, Categorical([0.6,0.4])))\n",
    "push!(bn, CategoricalCPD{Bernoulli}(:c, [:a, :b], [2,2], [Bernoulli(0.1), Bernoulli(0.2), Bernoulli(1.0), Bernoulli(0.4)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th><th>c</th></tr></thead><tbody><tr><th>1</th><td>2</td><td>2</td><td>1</td></tr><tr><th>2</th><td>2</td><td>1</td><td>1</td></tr><tr><th>3</th><td>2</td><td>1</td><td>1</td></tr><tr><th>4</th><td>2</td><td>1</td><td>1</td></tr><tr><th>5</th><td>2</td><td>2</td><td>1</td></tr></tbody></table>"
      ],
      "text/plain": [
       "5×3 DataFrames.DataFrame\n",
       "│ Row │ a │ b │ c │\n",
       "├─────┼───┼───┼───┤\n",
       "│ 1   │ 2 │ 2 │ 1 │\n",
       "│ 2   │ 2 │ 1 │ 1 │\n",
       "│ 3   │ 2 │ 1 │ 1 │\n",
       "│ 4   │ 2 │ 1 │ 1 │\n",
       "│ 5   │ 2 │ 2 │ 1 │"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand(bn, RejectionSampler(:c=>1), 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can also use weighted sampling:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th><th>c</th><th>p</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>1</td><td>1</td><td>0.09090909090909091</td></tr><tr><th>2</th><td>2</td><td>1</td><td>1</td><td>0.18181818181818182</td></tr><tr><th>3</th><td>2</td><td>1</td><td>1</td><td>0.18181818181818182</td></tr><tr><th>4</th><td>2</td><td>2</td><td>1</td><td>0.36363636363636365</td></tr><tr><th>5</th><td>2</td><td>1</td><td>1</td><td>0.18181818181818182</td></tr></tbody></table>"
      ],
      "text/plain": [
       "5×4 DataFrames.DataFrame\n",
       "│ Row │ a │ b │ c │ p         │\n",
       "├─────┼───┼───┼───┼───────────┤\n",
       "│ 1   │ 1 │ 1 │ 1 │ 0.0909091 │\n",
       "│ 2   │ 2 │ 1 │ 1 │ 0.181818  │\n",
       "│ 3   │ 2 │ 1 │ 1 │ 0.181818  │\n",
       "│ 4   │ 2 │ 2 │ 1 │ 0.363636  │\n",
       "│ 5   │ 2 │ 1 │ 1 │ 0.181818  │"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand(bn, LikelihoodWeightedSampler(:c=>1), 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can also use Gibbs sampling.  More options are available than are shown in the example below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th><th>c</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>9.240652298406218</td><td>10.0</td></tr><tr><th>2</th><td>1</td><td>6.037264154687797</td><td>10.0</td></tr><tr><th>3</th><td>1</td><td>6.841756268244145</td><td>10.0</td></tr><tr><th>4</th><td>1</td><td>9.50983660700297</td><td>10.0</td></tr><tr><th>5</th><td>1</td><td>8.568531949095366</td><td>10.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "5×3 DataFrames.DataFrame\n",
       "│ Row │ a │ b       │ c    │\n",
       "├─────┼───┼─────────┼──────┤\n",
       "│ 1   │ 1 │ 9.24065 │ 10.0 │\n",
       "│ 2   │ 1 │ 6.03726 │ 10.0 │\n",
       "│ 3   │ 1 │ 6.84176 │ 10.0 │\n",
       "│ 4   │ 1 │ 9.50984 │ 10.0 │\n",
       "│ 5   │ 1 │ 8.56853 │ 10.0 │"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bn_gibbs = BayesNet()\n",
    "push!(bn_gibbs, StaticCPD(:a, Categorical([0.999,0.001])))\n",
    "push!(bn_gibbs, StaticCPD(:b, Normal(1.0)))\n",
    "push!(bn_gibbs, LinearGaussianCPD(:c, [:a, :b], [3.0, 1.0], 0.0, 1.0))\n",
    "\n",
    "evidence = Assignment(:c => 10.0)\n",
    "initial_sample = Nullable{Assignment}(Assignment(:a => 1, :b => 1, :c => 10.0))\n",
    "sampler = GibbsSampler(evidence, burn_in=500, thinning=1, initial_sample=initial_sample)\n",
    "rand(bn_gibbs, sampler, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parameter Learning\n",
    "\n",
    "BayesNets.jl supports parameter learning for an entire graph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"12.18pt\" height=\"40.786pt\" viewBox=\"0 0 12.18 40.786\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066005-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066005-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066005-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -0.00015625 -5.807063 L -0.00015625 -20.857844 \" transform=\"matrix(1,0,0,-1,6.09,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194299 1.593594 C -1.096643 0.995938 0.00101375 0.101406 0.297889 -0.00015625 C 0.00101375 -0.0978125 -1.096643 -0.99625 -1.194299 -1.593906 \" transform=\"matrix(0,1,1,0,6.09,26.46383)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066005-0-1\" x=\"3.599\" y=\"7.784\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066005-0-2\" x=\"3.321\" y=\"37.356\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CPD}({2, 1} directed simple Int64 graph, BayesNets.CPDs.CPD[BayesNets.CPDs.StaticCPD{Distributions.Normal{Float64}}(:a, Symbol[], Distributions.Normal{Float64}(μ=1.16667, σ=0.62361)), BayesNets.CPDs.LinearGaussianCPD(:b, Symbol[:a], [2.0], 3.0, 1.52753)], Dict(:a=>1,:b=>2))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# specify each node's CPD type individually\n",
    "fit(BayesNet, data, (:a=>:b), [StaticCPD{Normal}, LinearGaussianCPD])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"12.18pt\" height=\"40.786pt\" viewBox=\"0 0 12.18 40.786\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066006-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066006-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066006-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -0.00015625 -5.807063 L -0.00015625 -20.857844 \" transform=\"matrix(1,0,0,-1,6.09,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194299 1.593594 C -1.096643 0.995938 0.00101375 0.101406 0.297889 -0.00015625 C 0.00101375 -0.0978125 -1.096643 -0.99625 -1.194299 -1.593906 \" transform=\"matrix(0,1,1,0,6.09,26.46383)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066006-0-1\" x=\"3.599\" y=\"7.784\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066006-0-2\" x=\"3.321\" y=\"37.356\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.LinearGaussianCPD}({2, 1} directed simple Int64 graph, BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:a, Symbol[], Float64[], 1.16667, 0.763763), BayesNets.CPDs.LinearGaussianCPD(:b, Symbol[:a], [2.0], 3.0, 1.52753)], Dict(:a=>1,:b=>2))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# specify a single CPD type for all nodes\n",
    "fit(BayesNet, data, (:a=>:b), LinearGaussianCPD)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fitting can be done for specific BayesNets types as well:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"40.248pt\" height=\"67.907pt\" viewBox=\"0 0 40.248 67.907\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066007-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066007-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066007-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066007-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.03125 -1.5625 4 -1.5625 C 3.921875 -1.5625 3.703125 -1.453125 3.671875 -1.390625 C 3.390625 -0.453125 2.9375 -0.40625 2.578125 -0.40625 C 2.046875 -0.40625 1.359375 -0.703125 1.359375 -2.296875 C 1.359375 -3.921875 1.984375 -4.21875 2.515625 -4.21875 C 2.609375 -4.21875 3.09375 -4.25 3.234375 -4.09375 C 3.171875 -4.09375 2.90625 -3.65625 2.90625 -3.515625 C 2.90625 -3.265625 3.296875 -2.9375 3.5625 -2.9375 C 3.828125 -2.9375 4.234375 -3.234375 4.234375 -3.53125 C 4.234375 -4.203125 3.265625 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.140625 -3.515625 0.140625 -2.28125 C 0.140625 -1 1.328125 0.109375 2.484375 0.109375 C 3.8125 0.109375 4.34375 -1.21875 4.34375 -1.3125 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -5.808094 -5.807062 L -21.733875 -21.732844 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195908 1.592938 C -1.096456 0.996331 0.000116531 0.0986846 0.298424 -0.000741962 C 0.000122156 -0.100185 -1.0964 -0.997894 -1.195818 -1.594507 \" transform=\"matrix(-0.70714,0.7071,0.7071,0.70714,12.70374,27.34029)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 0.0005 -5.807062 L 0.0005 -50.428156 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196216 1.59425 C -1.094654 0.996594 -0.00090375 0.0981563 0.299877 0.0005 C -0.00090375 -0.101062 -1.094654 -0.995594 -1.196216 -1.59325 \" transform=\"matrix(0,1,1,0,34.437,56.03606)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -22.058094 -34.635187 L -6.054187 -50.639094 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194248 1.594006 C -1.094814 0.997374 0.00176585 0.0996569 0.300081 0.000216201 C 0.00176444 -0.0992202 -1.094828 -0.996922 -1.194271 -1.593552 \" transform=\"matrix(0.7071,0.70709,0.70709,-0.7071,28.38141,56.245)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066007-0-1\" x=\"31.946\" y=\"7.784\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066007-0-2\" x=\"3.321\" y=\"37.356\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066007-0-3\" x=\"32.225\" y=\"64.477\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}}({3, 3} directed simple Int64 graph, BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}[2 instantiations:\n",
       "  a (2), 4 instantiations:\n",
       "  b (2)\n",
       "  a (2), 12 instantiations:\n",
       "  c (3)\n",
       "  b (2)\n",
       "  a (2)], Dict(:a=>1,:b=>2,:c=>3))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3], \n",
    "                 b=[1,1,1,2,2,2,2,1,1,2,1,1],\n",
    "                 a=[1,1,1,2,1,1,2,1,1,2,1,1])\n",
    "\n",
    "fit(DiscreteBayesNet, data, (:a=>:b, :a=>:c, :b=>:c))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fitting a `DiscreteCPD`, which is a `CategoricalCPD{Categorical}`, can be done with a specified number of categories. This prevents cases where your test data does not provide an example for every category."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cpd = fit(DiscreteCPD, DataFrame(a=[1,2,1,2,2]), :a, ncategories=3);\n",
    "cpd = fit(DiscreteCPD, data, :b, [:a], parental_ncategories=[3], target_ncategories=3);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inference\n",
    "\n",
    "Inference methods for discrete Bayesian networks can be used via the `infer` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"40.248pt\" height=\"40.786pt\" viewBox=\"0 0 40.248 40.786\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066008-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066008-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066008-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066008-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.03125 -1.5625 4 -1.5625 C 3.921875 -1.5625 3.703125 -1.453125 3.671875 -1.390625 C 3.390625 -0.453125 2.9375 -0.40625 2.578125 -0.40625 C 2.046875 -0.40625 1.359375 -0.703125 1.359375 -2.296875 C 1.359375 -3.921875 1.984375 -4.21875 2.515625 -4.21875 C 2.609375 -4.21875 3.09375 -4.25 3.234375 -4.09375 C 3.171875 -4.09375 2.90625 -3.65625 2.90625 -3.515625 C 2.90625 -3.265625 3.296875 -2.9375 3.5625 -2.9375 C 3.828125 -2.9375 4.234375 -3.234375 4.234375 -3.53125 C 4.234375 -4.203125 3.265625 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.140625 -3.515625 0.140625 -2.28125 C 0.140625 -1 1.328125 0.109375 2.484375 0.109375 C 3.8125 0.109375 4.34375 -1.21875 4.34375 -1.3125 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 2.903844 -5.807625 L 11.064 -22.127937 \" transform=\"matrix(1,0,0,-1,5.811,6.833)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196486 1.593872 C -1.095579 0.995324 0.000581384 0.0992235 0.298113 -0.00169962 C 0.000577819 -0.100872 -1.095614 -0.996933 -1.194803 -1.595478 \" transform=\"matrix(0.44902,0.898,0.898,-0.44902,16.87548,28.962)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 24.829625 -7.030281 L 17.28275 -22.127937 \" transform=\"matrix(1,0,0,-1,5.811,6.833)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194772 1.595487 C -1.095593 0.996935 0.000592984 0.100846 0.298129 0.00166759 C 0.000592984 -0.0992511 -1.095593 -0.99534 -1.196512 -1.593892 \" transform=\"matrix(-0.449,0.898,0.898,0.449,23.0933,28.962)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066008-0-1\" x=\"3.321\" y=\"9.009\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066008-0-2\" x=\"31.388\" y=\"10.235\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066008-0-3\" x=\"17.773\" y=\"37.356\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}}({3, 2} directed simple Int64 graph, BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}[2 instantiations:\n",
       "  a (2), 2 instantiations:\n",
       "  b (2), 8 instantiations:\n",
       "  c (2)\n",
       "  a (2)\n",
       "  b (2)], Dict(:a=>1,:b=>2,:c=>3))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bn = DiscreteBayesNet()\n",
    "push!(bn, DiscreteCPD(:a, [0.3,0.7]))\n",
    "push!(bn, DiscreteCPD(:b, [0.2,0.8]))\n",
    "push!(bn, DiscreteCPD(:c, [:a, :b], [2,2], \n",
    "        [Categorical([0.1,0.9]),\n",
    "         Categorical([0.2,0.8]),\n",
    "         Categorical([1.0,0.0]),\n",
    "         Categorical([0.4,0.6]),\n",
    "        ]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>c</th><th>potential</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>0.16999999999999996</td></tr><tr><th>2</th><td>2</td><td>0.83</td></tr></tbody></table>"
      ],
      "text/plain": [
       "2 instantiations:\n",
       "  c (2)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ϕ = infer(bn, :c, evidence=Assignment(:b=>1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Several inference methods are available. Exact inference is the default.\n",
    "\n",
    "| `InfereceMethod`               | Description |\n",
    "| ------------------------------ | ----------- |\n",
    "| `ExactInference`               | Performs exact inference using discrete factors and variable elimination |\n",
    "| `LikelihoodWeightingInference` | Approximates p(query \\ evidence) with N weighted samples using likelihood weighted sampling |\n",
    "| `LoopyBelief`                  | The loopy belief propagation algorithm |\n",
    "| `GibbsSamplingNodewise`        | Gibbs sampling where each iteration changes one node |\n",
    "| `GibbsSamplingFull`            | Gibbs sampling where each iteration changes all nodes | \n",
    "\n",
    "All inference methods inherit from the `InferenceMethod` type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th><th>potential</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>1</td><td>0.13</td></tr><tr><th>2</th><td>2</td><td>1</td><td>0.224</td></tr><tr><th>3</th><td>1</td><td>2</td><td>0.0</td></tr><tr><th>4</th><td>2</td><td>2</td><td>0.646</td></tr></tbody></table>"
      ],
      "text/plain": [
       "4 instantiations:\n",
       "  a (2)\n",
       "  b (2)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ϕ = infer(GibbsSamplingNodewise(), bn, [:a, :b], evidence=Assignment(:c=>2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inference produces a `Factor` type. It can be converted to a DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th><th>potential</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>1</td><td>0.13</td></tr><tr><th>2</th><td>2</td><td>1</td><td>0.224</td></tr><tr><th>3</th><td>1</td><td>2</td><td>0.0</td></tr><tr><th>4</th><td>2</td><td>2</td><td>0.646</td></tr></tbody></table>"
      ],
      "text/plain": [
       "4×3 DataFrames.DataFrame\n",
       "│ Row │ a │ b │ potential │\n",
       "├─────┼───┼───┼───────────┤\n",
       "│ 1   │ 1 │ 1 │ 0.13      │\n",
       "│ 2   │ 2 │ 1 │ 0.224     │\n",
       "│ 3   │ 1 │ 2 │ 0.0       │\n",
       "│ 4   │ 2 │ 2 │ 0.646     │"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "convert(DataFrame, ϕ)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Structure Learning\n",
    "\n",
    "Structure learning can be done as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>SepalLength</th><th>SepalWidth</th><th>PetalLength</th><th>PetalWidth</th><th>Species</th></tr></thead><tbody><tr><th>1</th><td>5.1</td><td>3.5</td><td>1.4</td><td>0.2</td><td>1</td></tr><tr><th>2</th><td>4.9</td><td>3.0</td><td>1.4</td><td>0.2</td><td>1</td></tr><tr><th>3</th><td>4.7</td><td>3.2</td><td>1.3</td><td>0.2</td><td>1</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ SepalLength │ SepalWidth │ PetalLength │ PetalWidth │ Species │\n",
       "├─────┼─────────────┼────────────┼─────────────┼────────────┼─────────┤\n",
       "│ 1   │ 5.1         │ 3.5        │ 1.4         │ 0.2        │ 1       │\n",
       "│ 2   │ 4.9         │ 3.0        │ 1.4         │ 0.2        │ 1       │\n",
       "│ 3   │ 4.7         │ 3.2        │ 1.3         │ 0.2        │ 1       │"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using Discretizers\n",
    "using RDatasets\n",
    "iris = dataset(\"datasets\", \"iris\")\n",
    "names(iris)\n",
    "data = DataFrame(\n",
    "    SepalLength = iris[:SepalLength],\n",
    "    SepalWidth = iris[:SepalWidth],\n",
    "    PetalLength = iris[:PetalLength],\n",
    "    PetalWidth = iris[:PetalWidth],\n",
    "    Species = encode(CategoricalDiscretizer(iris[:Species]), iris[:Species]),\n",
    ")\n",
    "data[1:3,:] # only display a subset..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we use the K2 structure learning algorithm which runs in polynomial time but requires that we specify a topological node ordering."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"153.718pt\" height=\"99.725pt\" viewBox=\"0 0 153.718 99.725\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.171875 -1.984375 C 5.171875 -2.984375 4.3125 -3.9375 3.484375 -4.140625 L 2.203125 -4.453125 C 1.578125 -4.59375 1.390625 -5 1.390625 -5.578125 C 1.390625 -6.265625 1.734375 -6.75 2.515625 -6.75 C 4.171875 -6.75 4.203125 -5.25 4.25 -4.796875 C 4.265625 -4.734375 4.46875 -4.546875 4.578125 -4.546875 C 4.703125 -4.546875 4.90625 -4.71875 4.90625 -4.921875 L 4.90625 -6.921875 C 4.90625 -7.09375 4.703125 -7.296875 4.59375 -7.296875 C 4.53125 -7.296875 4.375 -7.25 4.3125 -7.125 L 4.0625 -6.734375 C 3.9375 -6.84375 3.390625 -7.296875 2.5 -7.296875 C 1.390625 -7.296875 0.359375 -6.28125 0.359375 -5.21875 C 0.359375 -4.390625 1.09375 -3.53125 1.859375 -3.265625 C 1.96875 -3.234375 2.484375 -3.109375 3.1875 -2.9375 C 3.453125 -2.875 3.609375 -2.84375 3.890625 -2.46875 C 4.09375 -2.21875 4.140625 -1.96875 4.140625 -1.640625 C 4.140625 -0.9375 3.84375 -0.359375 3 -0.359375 C 2.71875 -0.359375 2.09375 -0.375 1.5625 -0.859375 C 0.984375 -1.390625 1.015625 -1.9375 1 -2.296875 C 1 -2.390625 0.71875 -2.53125 0.6875 -2.53125 C 0.5625 -2.53125 0.359375 -2.328125 0.359375 -2.140625 L 0.359375 -0.15625 C 0.359375 0.015625 0.5625 0.21875 0.671875 0.21875 C 0.734375 0.21875 0.890625 0.15625 0.953125 0.046875 C 0.953125 0.046875 0.984375 0.015625 1.203125 -0.328125 C 1.34375 -0.171875 2.125 0.21875 3.015625 0.21875 C 4.171875 0.21875 5.171875 -0.890625 5.171875 -1.984375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.234375 -4.671875 3.109375 -4.671875 C 2.328125 -4.671875 1.78125 -4.203125 1.578125 -3.984375 L 1.90625 -3.875 L 1.90625 -4.6875 L 0.078125 -4.546875 L 0.078125 -3.984375 C 0.984375 -3.984375 0.859375 -4.0625 0.859375 -3.625 L 0.859375 1.046875 C 0.859375 1.5 0.953125 1.359375 0.078125 1.359375 L 0.078125 1.9375 L 1.390625 1.90625 L 2.71875 1.9375 L 2.71875 1.359375 C 1.859375 1.359375 1.9375 1.5 1.9375 1.046875 L 1.9375 -0.71875 L 1.546875 -0.71875 C 1.59375 -0.5625 2.21875 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.28125 C 4.171875 -1.078125 3.671875 -0.375 2.9375 -0.375 C 2.53125 -0.375 2.296875 -0.53125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.0625 1.9375 -1.265625 L 1.9375 -3.5 C 2.171875 -3.90625 2.515625 -4.15625 3.03125 -4.15625 C 3.765625 -4.15625 4.171875 -3.40625 4.171875 -2.28125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.0625 -1.578125 4 -1.578125 C 3.921875 -1.578125 3.703125 -1.453125 3.671875 -1.359375 C 3.328125 -0.34375 2.625 -0.40625 2.53125 -0.40625 C 2.03125 -0.40625 1.78125 -0.671875 1.546875 -1.03125 C 1.25 -1.515625 1.3125 -2.078125 1.3125 -2.296875 L 3.890625 -2.296875 C 4.109375 -2.296875 4.34375 -2.4375 4.34375 -2.640625 C 4.34375 -3.625 3.59375 -4.734375 2.359375 -4.734375 C 1.203125 -4.734375 0.078125 -3.5625 0.078125 -2.328125 C 0.078125 -0.984375 1.328125 0.109375 2.46875 0.109375 C 3.6875 0.109375 4.34375 -1.125 4.34375 -1.3125 Z M 3.484375 -2.78125 L 1.3125 -2.78125 C 1.375 -4.125 2.015625 -4.25 2.359375 -4.25 C 3.375 -4.25 3.28125 -3.03125 3.28125 -2.78125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.03125 -1.5625 4 -1.5625 C 3.921875 -1.5625 3.703125 -1.453125 3.671875 -1.390625 C 3.390625 -0.453125 2.9375 -0.40625 2.578125 -0.40625 C 2.046875 -0.40625 1.359375 -0.703125 1.359375 -2.296875 C 1.359375 -3.921875 1.984375 -4.21875 2.515625 -4.21875 C 2.609375 -4.21875 3.09375 -4.25 3.234375 -4.09375 C 3.171875 -4.09375 2.90625 -3.65625 2.90625 -3.515625 C 2.90625 -3.265625 3.296875 -2.9375 3.5625 -2.9375 C 3.828125 -2.9375 4.234375 -3.234375 4.234375 -3.53125 C 4.234375 -4.203125 3.265625 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.140625 -3.515625 0.140625 -2.28125 C 0.140625 -1 1.328125 0.109375 2.484375 0.109375 C 3.8125 0.109375 4.34375 -1.21875 4.34375 -1.3125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 2.65625 -0.125 L 2.65625 -0.578125 C 1.796875 -0.578125 1.96875 -0.484375 1.96875 -0.875 L 1.96875 -4.6875 L 0.171875 -4.546875 L 0.171875 -3.984375 C 1.015625 -3.984375 0.90625 -4.0625 0.90625 -3.5625 L 0.90625 -0.890625 C 0.90625 -0.4375 1 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.421875 -0.03125 C 1.78125 -0.03125 2.125 -0.015625 2.65625 0.015625 Z M 2.109375 -6.15625 C 2.109375 -6.421875 1.6875 -6.8125 1.390625 -6.8125 C 1.046875 -6.8125 0.640625 -6.40625 0.640625 -6.15625 C 0.640625 -5.875 1.078125 -5.5 1.375 -5.5 C 1.71875 -5.5 2.109375 -5.90625 2.109375 -6.15625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-6\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.78125 -1.40625 C 3.78125 -1.9375 3.421875 -2.328125 3.3125 -2.453125 C 2.984375 -2.765625 2.453125 -2.890625 2.03125 -2.96875 C 1.46875 -3.078125 1 -3.078125 1 -3.65625 C 1 -4 1.0625 -4.28125 1.921875 -4.28125 C 3.015625 -4.28125 2.875 -3.515625 2.890625 -3.203125 C 2.90625 -3.109375 3.34375 -3.015625 3.34375 -3.015625 L 3.203125 -2.984375 C 3.34375 -2.984375 3.53125 -3.15625 3.53125 -3.34375 L 3.53125 -4.359375 C 3.53125 -4.53125 3.34375 -4.734375 3.234375 -4.734375 C 3.1875 -4.734375 3.015625 -4.6875 2.890625 -4.578125 C 2.859375 -4.53125 2.765625 -4.453125 2.84375 -4.515625 C 2.625 -4.6875 2.078125 -4.734375 1.921875 -4.734375 C 0.703125 -4.734375 0.125 -3.921875 0.125 -3.375 C 0.125 -3.015625 0.34375 -2.65625 0.625 -2.4375 C 0.9375 -2.171875 1.359375 -2.078125 2.078125 -1.9375 C 2.296875 -1.890625 2.90625 -1.859375 2.90625 -1.140625 C 2.90625 -0.640625 2.765625 -0.375 1.984375 -0.375 C 1.140625 -0.375 0.984375 -0.8125 0.796875 -1.65625 C 0.765625 -1.78125 0.5625 -1.96875 0.453125 -1.96875 C 0.328125 -1.96875 0.125 -1.75 0.125 -1.578125 L 0.125 -0.265625 C 0.125 -0.09375 0.328125 0.109375 0.4375 0.109375 C 0.484375 0.109375 0.640625 0.0625 0.828125 -0.125 C 0.84375 -0.15625 0.84375 -0.171875 0.890625 -0.21875 C 1.1875 0.0625 1.78125 0.109375 1.984375 0.109375 C 3.125 0.109375 3.78125 -0.6875 3.78125 -1.40625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-7\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-8\">\n",
       "<path style=\"stroke:none;\" d=\"M 2.734375 -0.125 L 2.734375 -0.578125 C 1.875 -0.578125 1.96875 -0.4375 1.96875 -0.890625 L 1.96875 -7.203125 L 0.125 -7.0625 L 0.125 -6.5 C 1.03125 -6.5 0.90625 -6.5625 0.90625 -6.078125 L 0.90625 -0.890625 C 0.90625 -0.4375 1 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.4375 -0.03125 L 2.734375 0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-9\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.796875 -2.84375 L 5.359375 -2.84375 C 5.25 -1.75 5.296875 -0.578125 3.546875 -0.578125 L 2.734375 -0.578125 C 2.265625 -0.578125 2.4375 -0.515625 2.4375 -0.84375 L 2.4375 -6.15625 C 2.4375 -6.484375 2.25 -6.5 3.1875 -6.5 L 3.71875 -6.5 L 3.71875 -7.09375 C 3.15625 -7.046875 2.25 -7.046875 1.84375 -7.046875 C 1.453125 -7.046875 0.671875 -7.046875 0.125 -7.09375 L 0.125 -6.5 L 0.5625 -6.5 C 1.328125 -6.5 1.15625 -6.515625 1.15625 -6.15625 L 1.15625 -0.90625 C 1.15625 -0.546875 1.328125 -0.578125 0.5625 -0.578125 L 0.125 -0.578125 L 0.125 0 L 5.703125 0 L 6.015625 -2.84375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-10\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.53125 -0.125 L 5.53125 -0.578125 C 4.8125 -0.578125 4.765625 -0.4375 4.75 -0.734375 L 4.75 -2.640625 C 4.75 -3.5 4.6875 -3.90625 4.390625 -4.265625 C 4.25 -4.4375 3.78125 -4.671875 3.203125 -4.671875 C 2.46875 -4.671875 1.796875 -4.171875 1.53125 -3.5625 L 1.921875 -3.484375 L 1.921875 -4.6875 L 0.125 -4.546875 L 0.125 -3.984375 C 1.015625 -3.984375 0.890625 -4.046875 0.890625 -3.5625 L 0.890625 -0.890625 C 0.890625 -0.4375 0.984375 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.453125 -0.03125 L 2.765625 0.015625 L 2.765625 -0.578125 C 1.890625 -0.578125 1.984375 -0.4375 1.984375 -0.890625 L 1.984375 -2.71875 C 1.984375 -3.765625 2.5 -4.1875 3.125 -4.1875 C 3.765625 -4.1875 3.671875 -3.78125 3.671875 -3.203125 L 3.671875 -0.890625 C 3.671875 -0.4375 3.765625 -0.578125 2.890625 -0.578125 L 2.890625 0.015625 L 4.21875 -0.03125 L 5.53125 0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-11\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.03125 -4.15625 C 5.03125 -4.328125 4.71875 -4.78125 4.328125 -4.78125 C 4.125 -4.78125 3.546875 -4.6875 3.25 -4.390625 C 2.984375 -4.609375 2.4375 -4.671875 2.21875 -4.671875 C 1.28125 -4.671875 0.40625 -3.84375 0.40625 -3.078125 C 0.40625 -2.640625 0.671875 -2.171875 0.828125 -2.046875 C 0.796875 -2 0.5625 -1.578125 0.5625 -1.21875 C 0.5625 -0.921875 0.75 -0.453125 0.84375 -0.375 C 0.59375 -0.3125 0.078125 0.265625 0.078125 0.65625 C 0.078125 1.375 1.265625 2.046875 2.484375 2.046875 C 3.65625 2.046875 4.890625 1.421875 4.890625 0.640625 C 4.890625 0.296875 4.6875 -0.3125 4.1875 -0.59375 C 3.65625 -0.875 2.9375 -0.921875 2.328125 -0.921875 C 2.078125 -0.921875 1.65625 -0.921875 1.578125 -0.921875 C 1.265625 -0.96875 1.25 -1.140625 1.25 -1.453125 C 1.25 -1.5 1.203125 -1.640625 1.25 -1.703125 C 1.46875 -1.5625 2.03125 -1.484375 2.21875 -1.484375 C 3.140625 -1.484375 4.03125 -2.296875 4.03125 -3.078125 C 4.03125 -3.4375 3.8125 -3.90625 3.65625 -4.03125 C 3.921875 -4.28125 4.140625 -4.296875 4.3125 -4.296875 L 4.453125 -4.34375 C 4.453125 -4.34375 4.390625 -4.296875 4.015625 -4.421875 C 4.3125 -4.515625 4.0625 -4.265625 4.0625 -4.140625 C 4.0625 -3.984375 4.390625 -3.734375 4.546875 -3.734375 C 4.640625 -3.734375 5.03125 -3.921875 5.03125 -4.15625 Z M 2.875 -3.078125 C 2.875 -2.8125 2.9375 -2.59375 2.78125 -2.34375 C 2.703125 -2.21875 2.609375 -1.984375 2.21875 -1.984375 C 1.34375 -1.984375 1.546875 -2.84375 1.546875 -3.078125 C 1.546875 -3.34375 1.5 -3.5625 1.640625 -3.8125 C 1.71875 -3.9375 1.8125 -4.171875 2.21875 -4.171875 C 3.078125 -4.171875 2.875 -3.3125 2.875 -3.078125 Z M 3.984375 0.65625 C 3.984375 1.203125 3.46875 1.5625 2.5 1.5625 C 1.484375 1.5625 1 1.1875 1 0.65625 C 1 0.203125 1.171875 -0.046875 1.609375 -0.0625 L 2.203125 -0.0625 C 3.0625 -0.0625 3.984375 -0.203125 3.984375 0.65625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-12\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.515625 -1.359375 L 3.515625 -2.078125 L 2.859375 -2.078125 L 2.859375 -1.390625 C 2.859375 -0.640625 2.765625 -0.40625 2.390625 -0.40625 C 1.71875 -0.40625 1.921875 -1.171875 1.921875 -1.34375 L 1.921875 -3.984375 L 3.34375 -3.984375 L 3.34375 -4.5625 L 1.921875 -4.5625 L 1.921875 -6.40625 L 1.28125 -6.40625 C 1.265625 -5.515625 1.171875 -4.515625 -0.015625 -4.46875 L -0.015625 -3.984375 L 0.84375 -3.984375 L 0.84375 -1.359375 C 0.84375 -0.140625 1.96875 0.109375 2.328125 0.109375 C 3.03125 0.109375 3.515625 -0.734375 3.515625 -1.359375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-13\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.53125 -0.125 L 5.53125 -0.578125 C 4.8125 -0.578125 4.765625 -0.4375 4.75 -0.734375 L 4.75 -2.640625 C 4.75 -3.5 4.6875 -3.90625 4.390625 -4.265625 C 4.25 -4.4375 3.78125 -4.671875 3.203125 -4.671875 C 2.359375 -4.671875 1.71875 -4 1.59375 -3.71875 L 1.953125 -3.71875 L 1.953125 -7.203125 L 0.125 -7.0625 L 0.125 -6.5 C 1.015625 -6.5 0.890625 -6.5625 0.890625 -6.078125 L 0.890625 -0.890625 C 0.890625 -0.4375 0.984375 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.453125 -0.03125 L 2.765625 0.015625 L 2.765625 -0.578125 C 1.890625 -0.578125 1.984375 -0.4375 1.984375 -0.890625 L 1.984375 -2.71875 C 1.984375 -3.765625 2.5 -4.1875 3.125 -4.1875 C 3.765625 -4.1875 3.671875 -3.78125 3.671875 -3.203125 L 3.671875 -0.890625 C 3.671875 -0.4375 3.765625 -0.578125 2.890625 -0.578125 L 2.890625 0.015625 L 4.21875 -0.03125 L 5.53125 0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-14\">\n",
       "<path style=\"stroke:none;\" d=\"M 6.421875 -5.078125 C 6.421875 -6.0625 5.234375 -7.078125 3.875 -7.078125 L 0.15625 -7.078125 L 0.15625 -6.5 L 0.59375 -6.5 C 1.359375 -6.5 1.171875 -6.515625 1.171875 -6.15625 L 1.171875 -0.90625 C 1.171875 -0.546875 1.359375 -0.578125 0.59375 -0.578125 L 0.15625 -0.578125 L 0.15625 0.015625 C 0.703125 -0.03125 1.4375 -0.03125 1.8125 -0.03125 C 2.1875 -0.03125 2.9375 -0.03125 3.484375 0.015625 L 3.484375 -0.578125 L 3.046875 -0.578125 C 2.28125 -0.578125 2.46875 -0.546875 2.46875 -0.90625 L 2.46875 -3.15625 L 3.953125 -3.15625 C 5.140625 -3.15625 6.421875 -4.09375 6.421875 -5.078125 Z M 5 -5.078125 C 5 -4.609375 5.1875 -3.671875 3.609375 -3.671875 L 2.4375 -3.671875 L 2.4375 -6.234375 C 2.4375 -6.5625 2.25 -6.5 2.71875 -6.5 L 3.609375 -6.5 C 5.1875 -6.5 5 -5.578125 5 -5.078125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-15\">\n",
       "<path style=\"stroke:none;\" d=\"M 10.265625 -6.625 L 10.265625 -7.09375 C 9.765625 -7.0625 9.453125 -7.046875 9.15625 -7.046875 L 7.765625 -7.09375 L 7.765625 -6.5 C 8.640625 -6.484375 8.640625 -6.296875 8.640625 -6.09375 C 8.640625 -6.046875 8.609375 -6.03125 8.578125 -5.96875 L 7.09375 -1.359375 L 7.484375 -1.296875 L 5.890625 -6.1875 C 5.875 -6.21875 5.859375 -6.28125 5.859375 -6.328125 C 5.859375 -6.625 6.234375 -6.5 6.703125 -6.5 L 6.703125 -7.09375 C 6.140625 -7.046875 5.46875 -7.046875 5.078125 -7.046875 L 3.671875 -7.078125 L 3.671875 -6.5 C 4.4375 -6.5 4.453125 -6.625 4.5625 -6.265625 L 4.78125 -5.609375 L 3.40625 -1.359375 L 3.796875 -1.296875 L 2.1875 -6.203125 C 2.1875 -6.234375 2.171875 -6.296875 2.171875 -6.328125 C 2.171875 -6.625 2.546875 -6.5 3.015625 -6.5 L 3.015625 -7.09375 C 2.453125 -7.046875 1.78125 -7.046875 1.390625 -7.046875 L -0.015625 -7.078125 L -0.015625 -6.5 C 0.921875 -6.5 0.765625 -6.578125 0.890625 -6.203125 L 2.875 -0.09375 C 2.90625 -0.015625 3.140625 0.21875 3.265625 0.21875 C 3.40625 0.21875 3.625 0.015625 3.671875 -0.109375 L 5.3125 -5.171875 L 4.921875 -5.171875 L 6.5625 -0.09375 C 6.59375 -0.015625 6.828125 0.21875 6.953125 0.21875 C 7.09375 0.21875 7.3125 0.015625 7.359375 -0.109375 L 9.265625 -5.96875 C 9.4375 -6.53125 9.671875 -6.5 10.265625 -6.5 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066009-0-16\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.453125 -0.125 L 5.453125 -0.578125 C 4.5625 -0.578125 4.671875 -0.515625 4.671875 -1 L 4.671875 -7.203125 L 2.84375 -7.0625 L 2.84375 -6.5 C 3.734375 -6.5 3.625 -6.5625 3.625 -6.078125 L 3.625 -3.921875 L 3.953125 -4.015625 C 3.671875 -4.375 3.09375 -4.671875 2.5625 -4.671875 C 1.390625 -4.671875 0.140625 -3.5625 0.140625 -2.265625 C 0.140625 -1 1.3125 0.109375 2.453125 0.109375 C 3.09375 0.109375 3.671875 -0.265625 3.921875 -0.59375 L 3.59375 -0.671875 L 3.59375 0.125 L 5.453125 -0.015625 Z M 3.59375 -1.3125 C 3.59375 -1.125 3.65625 -1.203125 3.53125 -1.03125 C 3.234375 -0.5625 2.9375 -0.375 2.5 -0.375 C 2.046875 -0.375 1.828125 -0.59375 1.59375 -0.984375 C 1.328125 -1.390625 1.359375 -1.859375 1.359375 -2.265625 C 1.359375 -2.625 1.328125 -3.140625 1.609375 -3.59375 C 1.828125 -3.890625 2.0625 -4.1875 2.609375 -4.1875 C 2.953125 -4.1875 3.234375 -4.078125 3.53125 -3.625 C 3.65625 -3.453125 3.59375 -3.53125 3.59375 -3.34375 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -17.521687 -7.997875 L -44.033406 -20.099438 \" transform=\"matrix(1,0,0,-1,92.342,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195214 1.592083 C -1.095532 0.994013 -0.000113386 0.0999402 0.298046 0.00240044 C -0.00171787 -0.100652 -1.096396 -0.997419 -1.197306 -1.593884 \" transform=\"matrix(-0.91193,0.41624,0.41624,0.91193,48.30986,27.89719)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -7.99825 -7.997875 L -28.345906 -28.345531 L -31.99825 -48.240063 \" transform=\"matrix(1,0,0,-1,92.342,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.19657 1.592588 C -1.095059 0.996011 -0.00211507 0.10107 0.297985 0.00133394 C -0.000745874 -0.1011 -1.094682 -0.996378 -1.19534 -1.594325 \" transform=\"matrix(-0.18056,0.98418,0.98418,0.18056,60.34546,56.03852)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 0.00175 -7.997875 L 0.00175 -77.493969 \" transform=\"matrix(1,0,0,-1,92.342,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196984 1.5955 C -1.095421 0.997844 -0.00167125 0.0994063 0.29911 0.00175 C -0.00167125 -0.0998125 -1.095421 -0.994344 -1.196984 -1.595906 \" transform=\"matrix(0,1,1,0,92.342,85.29464)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 8.575969 -7.997875 L 30.396281 -28.345531 L 31.833781 -48.240063 \" transform=\"matrix(1,0,0,-1,92.342,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.19497 1.592558 C -1.096983 0.998091 0.00111522 0.10027 0.299332 0.000388896 C -0.00156374 -0.0992464 -1.096222 -0.995149 -1.194419 -1.59501 \" transform=\"matrix(0.07213,0.99754,0.99754,-0.07213,124.17724,56.03737)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -53.986531 -36.404125 L -41.93575 -48.368969 \" transform=\"matrix(1,0,0,-1,92.342,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195881 1.591762 C -1.09599 0.998337 -0.00142386 0.100606 0.298866 -0.00255408 C 0.000597959 -0.100766 -1.09501 -0.996003 -1.193743 -1.594218 \" transform=\"matrix(0.71065,0.70547,0.70547,-0.71065,50.4066,56.16625)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -35.224812 -36.404125 L 5.329875 -48.564281 \" transform=\"matrix(1,0,0,-1,92.342,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196585 1.592758 C -1.095188 0.996811 -0.000875732 0.100677 0.298005 -0.000872855 C -0.00202934 -0.0989615 -1.095529 -0.996214 -1.196384 -1.595858 \" transform=\"matrix(0.96043,0.28795,0.28795,-0.96043,97.67107,56.36257)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -24.076375 -64.697094 L -8.736531 -77.658031 \" transform=\"matrix(1,0,0,-1,92.342,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.197671 1.594026 C -1.095789 0.99651 -0.0014887 0.0999485 0.297054 0.000211932 C -0.00162211 -0.0991244 -1.094608 -0.997195 -1.19729 -1.594116 \" transform=\"matrix(0.7657,0.64702,0.64702,-0.7657,83.60444,85.4564)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-1\" x=\"76.821\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-2\" x=\"82.360228\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-3\" x=\"87.899456\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-4\" x=\"92.322868\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-5\" x=\"96.74628\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-3\" x=\"99.515894\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-6\" x=\"103.939306\" y=\"10.344\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-1\" x=\"3.321\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-3\" x=\"8.860228\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-2\" x=\"13.28364\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-7\" x=\"18.822868\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-8\" x=\"23.804188\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-9\" x=\"26.573802\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-3\" x=\"32.800452\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-10\" x=\"37.223864\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-11\" x=\"42.763092\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-12\" x=\"47.744412\" y=\"38.631\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-13\" x=\"51.619879\" y=\"38.631\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-14\" x=\"32.085\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-3\" x=\"38.869558\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-12\" x=\"43.29297\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-7\" x=\"47.168437\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-8\" x=\"52.149757\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-9\" x=\"54.919371\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-3\" x=\"61.146021\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-10\" x=\"65.569433\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-11\" x=\"71.108661\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-12\" x=\"76.089981\" y=\"66.922\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-13\" x=\"79.965448\" y=\"66.922\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-14\" x=\"66.943\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-3\" x=\"73.727558\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-12\" x=\"78.15097\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-7\" x=\"82.026437\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-8\" x=\"87.007757\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-15\" x=\"89.777371\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-5\" x=\"100.018965\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-16\" x=\"102.788579\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-12\" x=\"108.327807\" y=\"96.185\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-13\" x=\"112.203274\" y=\"96.185\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-1\" x=\"99.18\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-3\" x=\"104.719228\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-2\" x=\"109.14264\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-7\" x=\"114.681868\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-8\" x=\"119.663188\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-15\" x=\"122.432802\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-5\" x=\"132.674396\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-16\" x=\"135.44401\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-12\" x=\"140.983237\" y=\"67.037\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066009-0-13\" x=\"144.858704\" y=\"67.037\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CPD}({5, 7} directed simple Int64 graph, BayesNets.CPDs.CPD[BayesNets.CPDs.ConditionalLinearGaussianCPD(:Species, Symbol[], Symbol[], Int64[], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:Species, Symbol[], Float64[], 2.0, 0.819232)]), BayesNets.CPDs.ConditionalLinearGaussianCPD(:SepalLength, Symbol[:Species], Symbol[:Species], [3], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:SepalLength, Symbol[], Float64[], 5.006, 0.35249), BayesNets.CPDs.LinearGaussianCPD(:SepalLength, Symbol[], Float64[], 5.936, 0.516171), BayesNets.CPDs.LinearGaussianCPD(:SepalLength, Symbol[], Float64[], 6.588, 0.63588)]), BayesNets.CPDs.ConditionalLinearGaussianCPD(:PetalLength, Symbol[:Species, :SepalLength], Symbol[:Species], [3], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:PetalLength, Symbol[:SepalLength], [0.131632], 0.803052, 0.173664), BayesNets.CPDs.LinearGaussianCPD(:PetalLength, Symbol[:SepalLength], [0.68647], 0.185116, 0.469911), BayesNets.CPDs.LinearGaussianCPD(:PetalLength, Symbol[:SepalLength], [0.750081], 0.610468, 0.551895)]), BayesNets.CPDs.ConditionalLinearGaussianCPD(:PetalWidth, Symbol[:Species, :PetalLength], Symbol[:Species], [3], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:PetalWidth, Symbol[:PetalLength], [0.201245], -0.0482203, 0.105386), BayesNets.CPDs.LinearGaussianCPD(:PetalWidth, Symbol[:PetalLength], [0.331054], -0.0842884, 0.197753), BayesNets.CPDs.LinearGaussianCPD(:PetalWidth, Symbol[:PetalLength], [0.160297], 1.13603, 0.27465)]), BayesNets.CPDs.ConditionalLinearGaussianCPD(:SepalWidth, Symbol[:Species, :SepalLength], Symbol[:Species], [3], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:SepalWidth, Symbol[:SepalLength], [0.798528], -0.569433, 0.379064), BayesNets.CPDs.LinearGaussianCPD(:SepalWidth, Symbol[:SepalLength], [0.319719], 0.872146, 0.313798), BayesNets.CPDs.LinearGaussianCPD(:SepalWidth, Symbol[:SepalLength], [0.23189], 1.44631, 0.322497)])], Dict(:SepalLength=>2,:PetalWidth=>4,:SepalWidth=>5,:PetalLength=>3,:Species=>1))"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "params = K2GraphSearch([:Species, :SepalLength, :SepalWidth, :PetalLength, :PetalWidth], \n",
    "                       ConditionalLinearGaussianCPD,\n",
    "                       max_n_parents=2)\n",
    "fit(BayesNet, data, params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "CPD types can also be specified per-node. Note that complete CPD definitions are required - simply using `StaticCPD` is insufficient as you need the target distribution type as well, as in `StaticCPD{Categorical}`.\n",
    "\n",
    "Changing the ordering will change the structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"152.881pt\" height=\"100.637pt\" viewBox=\"0 0 152.881 100.637\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.171875 -1.984375 C 5.171875 -2.984375 4.3125 -3.9375 3.484375 -4.140625 L 2.203125 -4.453125 C 1.578125 -4.59375 1.390625 -5 1.390625 -5.578125 C 1.390625 -6.265625 1.734375 -6.75 2.515625 -6.75 C 4.171875 -6.75 4.203125 -5.25 4.25 -4.796875 C 4.265625 -4.734375 4.46875 -4.546875 4.578125 -4.546875 C 4.703125 -4.546875 4.90625 -4.71875 4.90625 -4.921875 L 4.90625 -6.921875 C 4.90625 -7.09375 4.703125 -7.296875 4.59375 -7.296875 C 4.53125 -7.296875 4.375 -7.25 4.3125 -7.125 L 4.0625 -6.734375 C 3.9375 -6.84375 3.390625 -7.296875 2.5 -7.296875 C 1.390625 -7.296875 0.359375 -6.28125 0.359375 -5.21875 C 0.359375 -4.390625 1.09375 -3.53125 1.859375 -3.265625 C 1.96875 -3.234375 2.484375 -3.109375 3.1875 -2.9375 C 3.453125 -2.875 3.609375 -2.84375 3.890625 -2.46875 C 4.09375 -2.21875 4.140625 -1.96875 4.140625 -1.640625 C 4.140625 -0.9375 3.84375 -0.359375 3 -0.359375 C 2.71875 -0.359375 2.09375 -0.375 1.5625 -0.859375 C 0.984375 -1.390625 1.015625 -1.9375 1 -2.296875 C 1 -2.390625 0.71875 -2.53125 0.6875 -2.53125 C 0.5625 -2.53125 0.359375 -2.328125 0.359375 -2.140625 L 0.359375 -0.15625 C 0.359375 0.015625 0.5625 0.21875 0.671875 0.21875 C 0.734375 0.21875 0.890625 0.15625 0.953125 0.046875 C 0.953125 0.046875 0.984375 0.015625 1.203125 -0.328125 C 1.34375 -0.171875 2.125 0.21875 3.015625 0.21875 C 4.171875 0.21875 5.171875 -0.890625 5.171875 -1.984375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.234375 -4.671875 3.109375 -4.671875 C 2.328125 -4.671875 1.78125 -4.203125 1.578125 -3.984375 L 1.90625 -3.875 L 1.90625 -4.6875 L 0.078125 -4.546875 L 0.078125 -3.984375 C 0.984375 -3.984375 0.859375 -4.0625 0.859375 -3.625 L 0.859375 1.046875 C 0.859375 1.5 0.953125 1.359375 0.078125 1.359375 L 0.078125 1.9375 L 1.390625 1.90625 L 2.71875 1.9375 L 2.71875 1.359375 C 1.859375 1.359375 1.9375 1.5 1.9375 1.046875 L 1.9375 -0.71875 L 1.546875 -0.71875 C 1.59375 -0.5625 2.21875 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.28125 C 4.171875 -1.078125 3.671875 -0.375 2.9375 -0.375 C 2.53125 -0.375 2.296875 -0.53125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.0625 1.9375 -1.265625 L 1.9375 -3.5 C 2.171875 -3.90625 2.515625 -4.15625 3.03125 -4.15625 C 3.765625 -4.15625 4.171875 -3.40625 4.171875 -2.28125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.0625 -1.578125 4 -1.578125 C 3.921875 -1.578125 3.703125 -1.453125 3.671875 -1.359375 C 3.328125 -0.34375 2.625 -0.40625 2.53125 -0.40625 C 2.03125 -0.40625 1.78125 -0.671875 1.546875 -1.03125 C 1.25 -1.515625 1.3125 -2.078125 1.3125 -2.296875 L 3.890625 -2.296875 C 4.109375 -2.296875 4.34375 -2.4375 4.34375 -2.640625 C 4.34375 -3.625 3.59375 -4.734375 2.359375 -4.734375 C 1.203125 -4.734375 0.078125 -3.5625 0.078125 -2.328125 C 0.078125 -0.984375 1.328125 0.109375 2.46875 0.109375 C 3.6875 0.109375 4.34375 -1.125 4.34375 -1.3125 Z M 3.484375 -2.78125 L 1.3125 -2.78125 C 1.375 -4.125 2.015625 -4.25 2.359375 -4.25 C 3.375 -4.25 3.28125 -3.03125 3.28125 -2.78125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.03125 -1.5625 4 -1.5625 C 3.921875 -1.5625 3.703125 -1.453125 3.671875 -1.390625 C 3.390625 -0.453125 2.9375 -0.40625 2.578125 -0.40625 C 2.046875 -0.40625 1.359375 -0.703125 1.359375 -2.296875 C 1.359375 -3.921875 1.984375 -4.21875 2.515625 -4.21875 C 2.609375 -4.21875 3.09375 -4.25 3.234375 -4.09375 C 3.171875 -4.09375 2.90625 -3.65625 2.90625 -3.515625 C 2.90625 -3.265625 3.296875 -2.9375 3.5625 -2.9375 C 3.828125 -2.9375 4.234375 -3.234375 4.234375 -3.53125 C 4.234375 -4.203125 3.265625 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.140625 -3.515625 0.140625 -2.28125 C 0.140625 -1 1.328125 0.109375 2.484375 0.109375 C 3.8125 0.109375 4.34375 -1.21875 4.34375 -1.3125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 2.65625 -0.125 L 2.65625 -0.578125 C 1.796875 -0.578125 1.96875 -0.484375 1.96875 -0.875 L 1.96875 -4.6875 L 0.171875 -4.546875 L 0.171875 -3.984375 C 1.015625 -3.984375 0.90625 -4.0625 0.90625 -3.5625 L 0.90625 -0.890625 C 0.90625 -0.4375 1 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.421875 -0.03125 C 1.78125 -0.03125 2.125 -0.015625 2.65625 0.015625 Z M 2.109375 -6.15625 C 2.109375 -6.421875 1.6875 -6.8125 1.390625 -6.8125 C 1.046875 -6.8125 0.640625 -6.40625 0.640625 -6.15625 C 0.640625 -5.875 1.078125 -5.5 1.375 -5.5 C 1.71875 -5.5 2.109375 -5.90625 2.109375 -6.15625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-6\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.78125 -1.40625 C 3.78125 -1.9375 3.421875 -2.328125 3.3125 -2.453125 C 2.984375 -2.765625 2.453125 -2.890625 2.03125 -2.96875 C 1.46875 -3.078125 1 -3.078125 1 -3.65625 C 1 -4 1.0625 -4.28125 1.921875 -4.28125 C 3.015625 -4.28125 2.875 -3.515625 2.890625 -3.203125 C 2.90625 -3.109375 3.34375 -3.015625 3.34375 -3.015625 L 3.203125 -2.984375 C 3.34375 -2.984375 3.53125 -3.15625 3.53125 -3.34375 L 3.53125 -4.359375 C 3.53125 -4.53125 3.34375 -4.734375 3.234375 -4.734375 C 3.1875 -4.734375 3.015625 -4.6875 2.890625 -4.578125 C 2.859375 -4.53125 2.765625 -4.453125 2.84375 -4.515625 C 2.625 -4.6875 2.078125 -4.734375 1.921875 -4.734375 C 0.703125 -4.734375 0.125 -3.921875 0.125 -3.375 C 0.125 -3.015625 0.34375 -2.65625 0.625 -2.4375 C 0.9375 -2.171875 1.359375 -2.078125 2.078125 -1.9375 C 2.296875 -1.890625 2.90625 -1.859375 2.90625 -1.140625 C 2.90625 -0.640625 2.765625 -0.375 1.984375 -0.375 C 1.140625 -0.375 0.984375 -0.8125 0.796875 -1.65625 C 0.765625 -1.78125 0.5625 -1.96875 0.453125 -1.96875 C 0.328125 -1.96875 0.125 -1.75 0.125 -1.578125 L 0.125 -0.265625 C 0.125 -0.09375 0.328125 0.109375 0.4375 0.109375 C 0.484375 0.109375 0.640625 0.0625 0.828125 -0.125 C 0.84375 -0.15625 0.84375 -0.171875 0.890625 -0.21875 C 1.1875 0.0625 1.78125 0.109375 1.984375 0.109375 C 3.125 0.109375 3.78125 -0.6875 3.78125 -1.40625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-7\">\n",
       "<path style=\"stroke:none;\" d=\"M 6.421875 -5.078125 C 6.421875 -6.0625 5.234375 -7.078125 3.875 -7.078125 L 0.15625 -7.078125 L 0.15625 -6.5 L 0.59375 -6.5 C 1.359375 -6.5 1.171875 -6.515625 1.171875 -6.15625 L 1.171875 -0.90625 C 1.171875 -0.546875 1.359375 -0.578125 0.59375 -0.578125 L 0.15625 -0.578125 L 0.15625 0.015625 C 0.703125 -0.03125 1.4375 -0.03125 1.8125 -0.03125 C 2.1875 -0.03125 2.9375 -0.03125 3.484375 0.015625 L 3.484375 -0.578125 L 3.046875 -0.578125 C 2.28125 -0.578125 2.46875 -0.546875 2.46875 -0.90625 L 2.46875 -3.15625 L 3.953125 -3.15625 C 5.140625 -3.15625 6.421875 -4.09375 6.421875 -5.078125 Z M 5 -5.078125 C 5 -4.609375 5.1875 -3.671875 3.609375 -3.671875 L 2.4375 -3.671875 L 2.4375 -6.234375 C 2.4375 -6.5625 2.25 -6.5 2.71875 -6.5 L 3.609375 -6.5 C 5.1875 -6.5 5 -5.578125 5 -5.078125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-8\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.515625 -1.359375 L 3.515625 -2.078125 L 2.859375 -2.078125 L 2.859375 -1.390625 C 2.859375 -0.640625 2.765625 -0.40625 2.390625 -0.40625 C 1.71875 -0.40625 1.921875 -1.171875 1.921875 -1.34375 L 1.921875 -3.984375 L 3.34375 -3.984375 L 3.34375 -4.5625 L 1.921875 -4.5625 L 1.921875 -6.40625 L 1.28125 -6.40625 C 1.265625 -5.515625 1.171875 -4.515625 -0.015625 -4.46875 L -0.015625 -3.984375 L 0.84375 -3.984375 L 0.84375 -1.359375 C 0.84375 -0.140625 1.96875 0.109375 2.328125 0.109375 C 3.03125 0.109375 3.515625 -0.734375 3.515625 -1.359375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-9\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-10\">\n",
       "<path style=\"stroke:none;\" d=\"M 2.734375 -0.125 L 2.734375 -0.578125 C 1.875 -0.578125 1.96875 -0.4375 1.96875 -0.890625 L 1.96875 -7.203125 L 0.125 -7.0625 L 0.125 -6.5 C 1.03125 -6.5 0.90625 -6.5625 0.90625 -6.078125 L 0.90625 -0.890625 C 0.90625 -0.4375 1 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.4375 -0.03125 L 2.734375 0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-11\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.796875 -2.84375 L 5.359375 -2.84375 C 5.25 -1.75 5.296875 -0.578125 3.546875 -0.578125 L 2.734375 -0.578125 C 2.265625 -0.578125 2.4375 -0.515625 2.4375 -0.84375 L 2.4375 -6.15625 C 2.4375 -6.484375 2.25 -6.5 3.1875 -6.5 L 3.71875 -6.5 L 3.71875 -7.09375 C 3.15625 -7.046875 2.25 -7.046875 1.84375 -7.046875 C 1.453125 -7.046875 0.671875 -7.046875 0.125 -7.09375 L 0.125 -6.5 L 0.5625 -6.5 C 1.328125 -6.5 1.15625 -6.515625 1.15625 -6.15625 L 1.15625 -0.90625 C 1.15625 -0.546875 1.328125 -0.578125 0.5625 -0.578125 L 0.125 -0.578125 L 0.125 0 L 5.703125 0 L 6.015625 -2.84375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-12\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.53125 -0.125 L 5.53125 -0.578125 C 4.8125 -0.578125 4.765625 -0.4375 4.75 -0.734375 L 4.75 -2.640625 C 4.75 -3.5 4.6875 -3.90625 4.390625 -4.265625 C 4.25 -4.4375 3.78125 -4.671875 3.203125 -4.671875 C 2.46875 -4.671875 1.796875 -4.171875 1.53125 -3.5625 L 1.921875 -3.484375 L 1.921875 -4.6875 L 0.125 -4.546875 L 0.125 -3.984375 C 1.015625 -3.984375 0.890625 -4.046875 0.890625 -3.5625 L 0.890625 -0.890625 C 0.890625 -0.4375 0.984375 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.453125 -0.03125 L 2.765625 0.015625 L 2.765625 -0.578125 C 1.890625 -0.578125 1.984375 -0.4375 1.984375 -0.890625 L 1.984375 -2.71875 C 1.984375 -3.765625 2.5 -4.1875 3.125 -4.1875 C 3.765625 -4.1875 3.671875 -3.78125 3.671875 -3.203125 L 3.671875 -0.890625 C 3.671875 -0.4375 3.765625 -0.578125 2.890625 -0.578125 L 2.890625 0.015625 L 4.21875 -0.03125 L 5.53125 0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-13\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.03125 -4.15625 C 5.03125 -4.328125 4.71875 -4.78125 4.328125 -4.78125 C 4.125 -4.78125 3.546875 -4.6875 3.25 -4.390625 C 2.984375 -4.609375 2.4375 -4.671875 2.21875 -4.671875 C 1.28125 -4.671875 0.40625 -3.84375 0.40625 -3.078125 C 0.40625 -2.640625 0.671875 -2.171875 0.828125 -2.046875 C 0.796875 -2 0.5625 -1.578125 0.5625 -1.21875 C 0.5625 -0.921875 0.75 -0.453125 0.84375 -0.375 C 0.59375 -0.3125 0.078125 0.265625 0.078125 0.65625 C 0.078125 1.375 1.265625 2.046875 2.484375 2.046875 C 3.65625 2.046875 4.890625 1.421875 4.890625 0.640625 C 4.890625 0.296875 4.6875 -0.3125 4.1875 -0.59375 C 3.65625 -0.875 2.9375 -0.921875 2.328125 -0.921875 C 2.078125 -0.921875 1.65625 -0.921875 1.578125 -0.921875 C 1.265625 -0.96875 1.25 -1.140625 1.25 -1.453125 C 1.25 -1.5 1.203125 -1.640625 1.25 -1.703125 C 1.46875 -1.5625 2.03125 -1.484375 2.21875 -1.484375 C 3.140625 -1.484375 4.03125 -2.296875 4.03125 -3.078125 C 4.03125 -3.4375 3.8125 -3.90625 3.65625 -4.03125 C 3.921875 -4.28125 4.140625 -4.296875 4.3125 -4.296875 L 4.453125 -4.34375 C 4.453125 -4.34375 4.390625 -4.296875 4.015625 -4.421875 C 4.3125 -4.515625 4.0625 -4.265625 4.0625 -4.140625 C 4.0625 -3.984375 4.390625 -3.734375 4.546875 -3.734375 C 4.640625 -3.734375 5.03125 -3.921875 5.03125 -4.15625 Z M 2.875 -3.078125 C 2.875 -2.8125 2.9375 -2.59375 2.78125 -2.34375 C 2.703125 -2.21875 2.609375 -1.984375 2.21875 -1.984375 C 1.34375 -1.984375 1.546875 -2.84375 1.546875 -3.078125 C 1.546875 -3.34375 1.5 -3.5625 1.640625 -3.8125 C 1.71875 -3.9375 1.8125 -4.171875 2.21875 -4.171875 C 3.078125 -4.171875 2.875 -3.3125 2.875 -3.078125 Z M 3.984375 0.65625 C 3.984375 1.203125 3.46875 1.5625 2.5 1.5625 C 1.484375 1.5625 1 1.1875 1 0.65625 C 1 0.203125 1.171875 -0.046875 1.609375 -0.0625 L 2.203125 -0.0625 C 3.0625 -0.0625 3.984375 -0.203125 3.984375 0.65625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-14\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.53125 -0.125 L 5.53125 -0.578125 C 4.8125 -0.578125 4.765625 -0.4375 4.75 -0.734375 L 4.75 -2.640625 C 4.75 -3.5 4.6875 -3.90625 4.390625 -4.265625 C 4.25 -4.4375 3.78125 -4.671875 3.203125 -4.671875 C 2.359375 -4.671875 1.71875 -4 1.59375 -3.71875 L 1.953125 -3.71875 L 1.953125 -7.203125 L 0.125 -7.0625 L 0.125 -6.5 C 1.015625 -6.5 0.890625 -6.5625 0.890625 -6.078125 L 0.890625 -0.890625 C 0.890625 -0.4375 0.984375 -0.578125 0.125 -0.578125 L 0.125 0.015625 L 1.453125 -0.03125 L 2.765625 0.015625 L 2.765625 -0.578125 C 1.890625 -0.578125 1.984375 -0.4375 1.984375 -0.890625 L 1.984375 -2.71875 C 1.984375 -3.765625 2.5 -4.1875 3.125 -4.1875 C 3.765625 -4.1875 3.671875 -3.78125 3.671875 -3.203125 L 3.671875 -0.890625 C 3.671875 -0.4375 3.765625 -0.578125 2.890625 -0.578125 L 2.890625 0.015625 L 4.21875 -0.03125 L 5.53125 0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-15\">\n",
       "<path style=\"stroke:none;\" d=\"M 10.265625 -6.625 L 10.265625 -7.09375 C 9.765625 -7.0625 9.453125 -7.046875 9.15625 -7.046875 L 7.765625 -7.09375 L 7.765625 -6.5 C 8.640625 -6.484375 8.640625 -6.296875 8.640625 -6.09375 C 8.640625 -6.046875 8.609375 -6.03125 8.578125 -5.96875 L 7.09375 -1.359375 L 7.484375 -1.296875 L 5.890625 -6.1875 C 5.875 -6.21875 5.859375 -6.28125 5.859375 -6.328125 C 5.859375 -6.625 6.234375 -6.5 6.703125 -6.5 L 6.703125 -7.09375 C 6.140625 -7.046875 5.46875 -7.046875 5.078125 -7.046875 L 3.671875 -7.078125 L 3.671875 -6.5 C 4.4375 -6.5 4.453125 -6.625 4.5625 -6.265625 L 4.78125 -5.609375 L 3.40625 -1.359375 L 3.796875 -1.296875 L 2.1875 -6.203125 C 2.1875 -6.234375 2.171875 -6.296875 2.171875 -6.328125 C 2.171875 -6.625 2.546875 -6.5 3.015625 -6.5 L 3.015625 -7.09375 C 2.453125 -7.046875 1.78125 -7.046875 1.390625 -7.046875 L -0.015625 -7.078125 L -0.015625 -6.5 C 0.921875 -6.5 0.765625 -6.578125 0.890625 -6.203125 L 2.875 -0.09375 C 2.90625 -0.015625 3.140625 0.21875 3.265625 0.21875 C 3.40625 0.21875 3.625 0.015625 3.671875 -0.109375 L 5.3125 -5.171875 L 4.921875 -5.171875 L 6.5625 -0.09375 C 6.59375 -0.015625 6.828125 0.21875 6.953125 0.21875 C 7.09375 0.21875 7.3125 0.015625 7.359375 -0.109375 L 9.265625 -5.96875 C 9.4375 -6.53125 9.671875 -6.5 10.265625 -6.5 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066010-0-16\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.453125 -0.125 L 5.453125 -0.578125 C 4.5625 -0.578125 4.671875 -0.515625 4.671875 -1 L 4.671875 -7.203125 L 2.84375 -7.0625 L 2.84375 -6.5 C 3.734375 -6.5 3.625 -6.5625 3.625 -6.078125 L 3.625 -3.921875 L 3.953125 -4.015625 C 3.671875 -4.375 3.09375 -4.671875 2.5625 -4.671875 C 1.390625 -4.671875 0.140625 -3.5625 0.140625 -2.265625 C 0.140625 -1 1.3125 0.109375 2.453125 0.109375 C 3.09375 0.109375 3.671875 -0.265625 3.921875 -0.59375 L 3.59375 -0.671875 L 3.59375 0.125 L 5.453125 -0.015625 Z M 3.59375 -1.3125 C 3.59375 -1.125 3.65625 -1.203125 3.53125 -1.03125 C 3.234375 -0.5625 2.9375 -0.375 2.5 -0.375 C 2.046875 -0.375 1.828125 -0.59375 1.59375 -0.984375 C 1.328125 -1.390625 1.359375 -1.859375 1.359375 -2.265625 C 1.359375 -2.625 1.328125 -3.140625 1.609375 -3.59375 C 1.828125 -3.890625 2.0625 -4.1875 2.609375 -4.1875 C 2.953125 -4.1875 3.234375 -4.078125 3.53125 -3.625 C 3.65625 -3.453125 3.59375 -3.53125 3.59375 -3.34375 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -17.463063 -7.997875 L -44.002125 -20.154125 \" transform=\"matrix(1,0,0,-1,91.924,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196868 1.595278 C -1.094468 0.995809 -0.000833192 0.100859 0.298946 -0.00216675 C 0.000847697 -0.101235 -1.094336 -0.996078 -1.195949 -1.592065 \" transform=\"matrix(-0.91193,0.41765,0.41765,0.91193,47.92196,27.95134)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -7.998219 -7.997875 L -28.345875 -28.345531 L -32.134938 -48.185375 \" transform=\"matrix(1,0,0,-1,91.924,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195194 1.595037 C -1.09706 0.997461 0.000360138 0.101517 0.298196 -0.000691433 C -0.000665806 -0.101462 -1.094727 -0.994145 -1.195004 -1.5938 \" transform=\"matrix(-0.18748,0.98242,0.98242,0.18748,59.79096,55.98452)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 0.00178125 -7.997875 L 0.00178125 -76.583812 \" transform=\"matrix(1,0,0,-1,91.924,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.19555 1.595531 C -1.093988 0.997875 -0.0002375 0.0994375 0.300544 0.00178125 C -0.0002375 -0.0997813 -1.093988 -0.994313 -1.19555 -1.595875 \" transform=\"matrix(0,1,1,0,91.924,84.38305)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 8.548656 -7.997875 L 30.290844 -28.345531 L 31.720531 -49.150219 \" transform=\"matrix(1,0,0,-1,91.924,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.197146 1.593422 C -1.097276 0.995664 -0.000480574 0.0986279 0.297982 -0.000113535 C 0.00147167 -0.101084 -1.096961 -0.996225 -1.196535 -1.592041 \" transform=\"matrix(0.06848,0.9982,0.9982,-0.06848,123.64377,56.94864)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -53.951344 -36.349437 L -42.08025 -48.310375 \" transform=\"matrix(1,0,0,-1,91.924,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.193064 1.594064 C -1.094311 0.995835 0.0013531 0.100604 0.296884 -0.000370928 C -0.000663071 -0.100774 -1.098003 -0.995813 -1.195111 -1.592006 \" transform=\"matrix(0.70543,0.71065,0.71065,-0.70543,49.84552,56.10907)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -35.318531 -36.349437 L 8.267406 -49.474437 \" transform=\"matrix(1,0,0,-1,91.924,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196455 1.593774 C -1.094113 0.996535 0.000112498 0.0984475 0.298695 0.000750306 C -0.000807659 -0.101665 -1.096962 -0.998597 -1.194733 -1.594916 \" transform=\"matrix(0.95782,0.2884,0.2884,-0.95782,100.19025,57.27395)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -24.162281 -64.751781 L -9.877125 -76.747875 \" transform=\"matrix(1,0,0,-1,91.924,7.799)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195536 1.592982 C -1.095725 0.996558 -0.00126937 0.0982726 0.299243 0.000355233 C 0.000964279 -0.0978319 -1.09566 -0.998417 -1.194453 -1.594097 \" transform=\"matrix(0.76949,0.64618,0.64618,-0.76949,82.04685,84.54519)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-1\" x=\"76.402\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-2\" x=\"81.941228\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-3\" x=\"87.480456\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-4\" x=\"91.903868\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-5\" x=\"96.32728\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-3\" x=\"99.096894\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-6\" x=\"103.520306\" y=\"10.344\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-7\" x=\"3.321\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-3\" x=\"10.105558\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-8\" x=\"14.52897\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-9\" x=\"18.404437\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-10\" x=\"23.385757\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-11\" x=\"26.155371\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-3\" x=\"32.382021\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-12\" x=\"36.805433\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-13\" x=\"42.344661\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-8\" x=\"47.325981\" y=\"38.576\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-14\" x=\"51.201448\" y=\"38.576\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-1\" x=\"31.249\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-3\" x=\"36.788228\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-2\" x=\"41.21164\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-9\" x=\"46.750868\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-10\" x=\"51.732188\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-11\" x=\"54.501802\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-3\" x=\"60.728452\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-12\" x=\"65.151864\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-13\" x=\"70.691092\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-8\" x=\"75.672412\" y=\"66.978\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-14\" x=\"79.547879\" y=\"66.978\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-1\" x=\"66.315\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-3\" x=\"71.854228\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-2\" x=\"76.27764\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-9\" x=\"81.816868\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-10\" x=\"86.798188\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-15\" x=\"89.567802\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-5\" x=\"99.809396\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-16\" x=\"102.57901\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-8\" x=\"108.118237\" y=\"95.384\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-14\" x=\"111.993704\" y=\"95.384\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-7\" x=\"98.761\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-3\" x=\"105.545558\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-8\" x=\"109.96897\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-9\" x=\"113.844437\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-10\" x=\"118.825757\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-15\" x=\"121.595371\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-5\" x=\"131.836965\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-16\" x=\"134.606579\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-8\" x=\"140.145807\" y=\"67.839\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066010-0-14\" x=\"144.021274\" y=\"67.839\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CPD}({5, 7} directed simple Int64 graph, BayesNets.CPDs.CPD[BayesNets.CPDs.StaticCPD{Distributions.Categorical{Float64}}(:Species, Symbol[], Distributions.Categorical{Float64}(K=3, p=[0.333333, 0.333333, 0.333333])), BayesNets.CPDs.ConditionalLinearGaussianCPD(:PetalLength, Symbol[:Species], Symbol[:Species], [3], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:PetalLength, Symbol[], Float64[], 1.462, 0.173664), BayesNets.CPDs.LinearGaussianCPD(:PetalLength, Symbol[], Float64[], 4.26, 0.469911), BayesNets.CPDs.LinearGaussianCPD(:PetalLength, Symbol[], Float64[], 5.552, 0.551895)]), BayesNets.CPDs.ConditionalLinearGaussianCPD(:SepalLength, Symbol[:Species, :PetalLength], Symbol[:Species], [3], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:SepalLength, Symbol[:PetalLength], [0.542293], 4.21317, 0.35249), BayesNets.CPDs.LinearGaussianCPD(:SepalLength, Symbol[:PetalLength], [0.828281], 2.40752, 0.516171), BayesNets.CPDs.LinearGaussianCPD(:SepalLength, Symbol[:PetalLength], [0.995739], 1.05966, 0.63588)]), BayesNets.CPDs.ConditionalLinearGaussianCPD(:SepalWidth, Symbol[:Species, :SepalLength], Symbol[:Species], [3], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:SepalWidth, Symbol[:SepalLength], [0.798528], -0.569433, 0.379064), BayesNets.CPDs.LinearGaussianCPD(:SepalWidth, Symbol[:SepalLength], [0.319719], 0.872146, 0.313798), BayesNets.CPDs.LinearGaussianCPD(:SepalWidth, Symbol[:SepalLength], [0.23189], 1.44631, 0.322497)]), BayesNets.CPDs.ConditionalLinearGaussianCPD(:PetalWidth, Symbol[:Species, :PetalLength], Symbol[:Species], [3], BayesNets.CPDs.LinearGaussianCPD[BayesNets.CPDs.LinearGaussianCPD(:PetalWidth, Symbol[:PetalLength], [0.201245], -0.0482203, 0.105386), BayesNets.CPDs.LinearGaussianCPD(:PetalWidth, Symbol[:PetalLength], [0.331054], -0.0842884, 0.197753), BayesNets.CPDs.LinearGaussianCPD(:PetalWidth, Symbol[:PetalLength], [0.160297], 1.13603, 0.27465)])], Dict(:SepalLength=>3,:SepalWidth=>4,:PetalWidth=>5,:PetalLength=>2,:Species=>1))"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "CLG = ConditionalLinearGaussianCPD\n",
    "params = K2GraphSearch([:Species, :PetalLength, :PetalWidth, :SepalLength, :SepalWidth], \n",
    "                        [StaticCPD{Categorical}, CLG, CLG, CLG, CLG],\n",
    "                        max_n_parents=2)\n",
    "fit(BayesNet, data, params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A `ScoringFunction` allows for extracting a scoring metric for a CPD given data.\n",
    "The negative BIC score is implemented in NegativeBayesianInformationCriterion.\n",
    "\n",
    "A `GraphSearchStrategy` defines a structure learning algorithm.\n",
    "The K2 algorithm is defined through `K2GraphSearch` and `GreedyHillClimbing` is implemented for discrete Bayesian networks and the Bayesian score:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"40.248pt\" height=\"67.907pt\" viewBox=\"0 0 40.248 67.907\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066011-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066011-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066011-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066011-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.03125 -1.5625 4 -1.5625 C 3.921875 -1.5625 3.703125 -1.453125 3.671875 -1.390625 C 3.390625 -0.453125 2.9375 -0.40625 2.578125 -0.40625 C 2.046875 -0.40625 1.359375 -0.703125 1.359375 -2.296875 C 1.359375 -3.921875 1.984375 -4.21875 2.515625 -4.21875 C 2.609375 -4.21875 3.09375 -4.25 3.234375 -4.09375 C 3.171875 -4.09375 2.90625 -3.65625 2.90625 -3.515625 C 2.90625 -3.265625 3.296875 -2.9375 3.5625 -2.9375 C 3.828125 -2.9375 4.234375 -3.234375 4.234375 -3.53125 C 4.234375 -4.203125 3.265625 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.140625 -3.515625 0.140625 -2.28125 C 0.140625 -1 1.328125 0.109375 2.484375 0.109375 C 3.8125 0.109375 4.34375 -1.21875 4.34375 -1.3125 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -5.808094 -5.807062 L -21.733875 -21.732844 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195908 1.592938 C -1.096456 0.996331 0.000116531 0.0986846 0.298424 -0.000741962 C 0.000122156 -0.100185 -1.0964 -0.997894 -1.195818 -1.594507 \" transform=\"matrix(-0.70714,0.7071,0.7071,0.70714,12.70374,27.34029)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 0.0005 -5.807062 L 0.0005 -50.428156 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196216 1.59425 C -1.094654 0.996594 -0.00090375 0.0981563 0.299877 0.0005 C -0.00090375 -0.101062 -1.094654 -0.995594 -1.196216 -1.59325 \" transform=\"matrix(0,1,1,0,34.437,56.03606)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -22.058094 -34.635187 L -6.054187 -50.639094 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194248 1.594006 C -1.094814 0.997374 0.00176585 0.0996569 0.300081 0.000216201 C 0.00176444 -0.0992202 -1.094828 -0.996922 -1.194271 -1.593552 \" transform=\"matrix(0.7071,0.70709,0.70709,-0.7071,28.38141,56.245)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066011-0-1\" x=\"31.946\" y=\"7.784\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066011-0-2\" x=\"3.321\" y=\"37.356\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066011-0-3\" x=\"32.225\" y=\"64.477\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}}({3, 3} directed simple Int64 graph, BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}[2 instantiations:\n",
       "  a (2), 4 instantiations:\n",
       "  b (2)\n",
       "  a (2), 12 instantiations:\n",
       "  c (3)\n",
       "  b (2)\n",
       "  a (2)], Dict(:a=>1,:b=>2,:c=>3))"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3], \n",
    "                 b=[1,1,1,2,2,2,2,1,1,2,1,1],\n",
    "                 a=[1,1,1,2,1,1,2,1,1,2,1,1])\n",
    "params = GreedyHillClimbing(ScoreComponentCache(data), max_n_parents=3, prior=UniformPrior())\n",
    "bn = fit(DiscreteBayesNet, data, params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can specify the number of categories for each variable in case it cannot be correctly inferred:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"40.248pt\" height=\"67.907pt\" viewBox=\"0 0 40.248 67.907\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066012-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066012-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066012-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.390625 -2.28125 C 5.390625 -3.546875 4.21875 -4.671875 3.078125 -4.671875 C 2.296875 -4.671875 1.734375 -4.171875 1.578125 -3.984375 L 1.90625 -3.890625 L 1.90625 -7.203125 L 0.078125 -7.0625 L 0.078125 -6.5 C 0.984375 -6.5 0.859375 -6.5625 0.859375 -6.078125 L 0.859375 0 L 1.421875 0 L 1.671875 -0.421875 C 1.671875 -0.421875 2.234375 0.109375 2.96875 0.109375 C 4.15625 0.109375 5.390625 -1 5.390625 -2.28125 Z M 4.171875 -2.296875 C 4.171875 -1.921875 4.203125 -1.421875 3.921875 -0.984375 C 3.703125 -0.671875 3.46875 -0.375 2.9375 -0.375 C 2.484375 -0.375 2.265625 -0.578125 2.03125 -0.953125 C 1.890625 -1.15625 1.9375 -1.09375 1.9375 -1.265625 L 1.9375 -3.3125 C 1.9375 -3.515625 1.890625 -3.421875 2 -3.59375 C 2.375 -4.140625 2.796875 -4.1875 3.03125 -4.1875 C 3.484375 -4.1875 3.703125 -3.96875 3.9375 -3.59375 C 4.203125 -3.1875 4.171875 -2.703125 4.171875 -2.296875 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066012-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.03125 -1.5625 4 -1.5625 C 3.921875 -1.5625 3.703125 -1.453125 3.671875 -1.390625 C 3.390625 -0.453125 2.9375 -0.40625 2.578125 -0.40625 C 2.046875 -0.40625 1.359375 -0.703125 1.359375 -2.296875 C 1.359375 -3.921875 1.984375 -4.21875 2.515625 -4.21875 C 2.609375 -4.21875 3.09375 -4.25 3.234375 -4.09375 C 3.171875 -4.09375 2.90625 -3.65625 2.90625 -3.515625 C 2.90625 -3.265625 3.296875 -2.9375 3.5625 -2.9375 C 3.828125 -2.9375 4.234375 -3.234375 4.234375 -3.53125 C 4.234375 -4.203125 3.265625 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.140625 -3.515625 0.140625 -2.28125 C 0.140625 -1 1.328125 0.109375 2.484375 0.109375 C 3.8125 0.109375 4.34375 -1.21875 4.34375 -1.3125 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -5.808094 -5.807062 L -21.733875 -21.732844 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.195908 1.592938 C -1.096456 0.996331 0.000116531 0.0986846 0.298424 -0.000741962 C 0.000122156 -0.100185 -1.0964 -0.997894 -1.195818 -1.594507 \" transform=\"matrix(-0.70714,0.7071,0.7071,0.70714,12.70374,27.34029)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 0.0005 -5.807062 L 0.0005 -50.428156 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196216 1.59425 C -1.094654 0.996594 -0.00090375 0.0981563 0.299877 0.0005 C -0.00090375 -0.101062 -1.094654 -0.995594 -1.196216 -1.59325 \" transform=\"matrix(0,1,1,0,34.437,56.03606)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -22.058094 -34.635187 L -6.054187 -50.639094 \" transform=\"matrix(1,0,0,-1,34.437,5.607)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194248 1.594006 C -1.094814 0.997374 0.00176585 0.0996569 0.300081 0.000216201 C 0.00176444 -0.0992202 -1.094828 -0.996922 -1.194271 -1.593552 \" transform=\"matrix(0.7071,0.70709,0.70709,-0.7071,28.38141,56.245)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066012-0-1\" x=\"31.946\" y=\"7.784\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066012-0-2\" x=\"3.321\" y=\"37.356\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066012-0-3\" x=\"32.225\" y=\"64.477\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}}({3, 3} directed simple Int64 graph, BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}[2 instantiations:\n",
       "  a (2), 6 instantiations:\n",
       "  b (3)\n",
       "  a (2), 18 instantiations:\n",
       "  c (3)\n",
       "  b (3)\n",
       "  a (2)], Dict(:a=>1,:b=>2,:c=>3))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bn = fit(DiscreteBayesNet, data, params, ncategories=[3,3,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A whole suite of features are supported for DiscreteBayesNets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-31.28804624550449"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bayesian_score(bn, data, params.prior) # compute the Bayesian score of the data under the BayesNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>count</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>9</td></tr><tr><th>2</th><td>2</td><td>3</td></tr></tbody></table>"
      ],
      "text/plain": [
       "2×2 DataFrames.DataFrame\n",
       "│ Row │ a │ count │\n",
       "├─────┼───┼───────┤\n",
       "│ 1   │ 1 │ 9     │\n",
       "│ 2   │ 2 │ 3     │"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count(bn, :a, data) # obtain a list of counts for the node"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Array{Int64,2},1}:\n",
       " [4; 4; 4]             \n",
       " [3 1 3; 1 3 1]        \n",
       " [3 1 … 2 0; 0 0 … 1 1]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "statistics(bn.dag, data) # sufficient statistics from a discrete dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>a</th><th>b</th><th>p</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>1</td><td>0.6666666666666666</td></tr><tr><th>2</th><td>2</td><td>1</td><td>0.16666666666666666</td></tr><tr><th>3</th><td>1</td><td>2</td><td>0.25</td></tr><tr><th>4</th><td>2</td><td>2</td><td>0.6666666666666666</td></tr><tr><th>5</th><td>1</td><td>3</td><td>0.08333333333333333</td></tr><tr><th>6</th><td>2</td><td>3</td><td>0.16666666666666666</td></tr></tbody></table>"
      ],
      "text/plain": [
       "BayesNets.Table(6×3 DataFrames.DataFrame\n",
       "│ Row │ a │ b │ p         │\n",
       "├─────┼───┼───┼───────────┤\n",
       "│ 1   │ 1 │ 1 │ 0.666667  │\n",
       "│ 2   │ 2 │ 1 │ 0.166667  │\n",
       "│ 3   │ 1 │ 2 │ 0.25      │\n",
       "│ 4   │ 2 │ 2 │ 0.666667  │\n",
       "│ 5   │ 1 │ 3 │ 0.0833333 │\n",
       "│ 6   │ 2 │ 3 │ 0.166667  │)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table(bn, :b) # obtain the factor table for a node"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>b</th><th>a</th><th>c</th><th>p</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>1</td><td>1</td><td>0.4</td></tr><tr><th>2</th><td>2</td><td>1</td><td>1</td><td>0.2</td></tr><tr><th>3</th><td>3</td><td>1</td><td>1</td><td>0.3333333333333333</td></tr><tr><th>4</th><td>1</td><td>1</td><td>2</td><td>0.2</td></tr><tr><th>5</th><td>2</td><td>1</td><td>2</td><td>0.6</td></tr><tr><th>6</th><td>3</td><td>1</td><td>2</td><td>0.3333333333333333</td></tr><tr><th>7</th><td>1</td><td>1</td><td>3</td><td>0.4</td></tr><tr><th>8</th><td>2</td><td>1</td><td>3</td><td>0.2</td></tr><tr><th>9</th><td>3</td><td>1</td><td>3</td><td>0.3333333333333333</td></tr></tbody></table>"
      ],
      "text/plain": [
       "BayesNets.Table(9×4 DataFrames.DataFrame\n",
       "│ Row │ b │ a │ c │ p        │\n",
       "├─────┼───┼───┼───┼──────────┤\n",
       "│ 1   │ 1 │ 1 │ 1 │ 0.4      │\n",
       "│ 2   │ 2 │ 1 │ 1 │ 0.2      │\n",
       "│ 3   │ 3 │ 1 │ 1 │ 0.333333 │\n",
       "│ 4   │ 1 │ 1 │ 2 │ 0.2      │\n",
       "│ 5   │ 2 │ 1 │ 2 │ 0.6      │\n",
       "│ 6   │ 3 │ 1 │ 2 │ 0.333333 │\n",
       "│ 7   │ 1 │ 1 │ 3 │ 0.4      │\n",
       "│ 8   │ 2 │ 1 │ 3 │ 0.2      │\n",
       "│ 9   │ 3 │ 1 │ 3 │ 0.333333 │)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table(bn, :c, :a=>1) # obtain a factor table matching a particular assignment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Reading from XDSL\n",
    "\n",
    "One can read discrete Bayesian networks from the .XDSL file format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"43.662pt\" height=\"42.051pt\" viewBox=\"0 0 43.662 42.051\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.171875 -1.984375 C 5.171875 -2.984375 4.3125 -3.9375 3.484375 -4.140625 L 2.203125 -4.453125 C 1.578125 -4.59375 1.390625 -5 1.390625 -5.578125 C 1.390625 -6.265625 1.734375 -6.75 2.515625 -6.75 C 4.171875 -6.75 4.203125 -5.25 4.25 -4.796875 C 4.265625 -4.734375 4.46875 -4.546875 4.578125 -4.546875 C 4.703125 -4.546875 4.90625 -4.71875 4.90625 -4.921875 L 4.90625 -6.921875 C 4.90625 -7.09375 4.703125 -7.296875 4.59375 -7.296875 C 4.53125 -7.296875 4.375 -7.25 4.3125 -7.125 L 4.0625 -6.734375 C 3.9375 -6.84375 3.390625 -7.296875 2.5 -7.296875 C 1.390625 -7.296875 0.359375 -6.28125 0.359375 -5.21875 C 0.359375 -4.390625 1.09375 -3.53125 1.859375 -3.265625 C 1.96875 -3.234375 2.484375 -3.109375 3.1875 -2.9375 C 3.453125 -2.875 3.609375 -2.84375 3.890625 -2.46875 C 4.09375 -2.21875 4.140625 -1.96875 4.140625 -1.640625 C 4.140625 -0.9375 3.84375 -0.359375 3 -0.359375 C 2.71875 -0.359375 2.09375 -0.375 1.5625 -0.859375 C 0.984375 -1.390625 1.015625 -1.9375 1 -2.296875 C 1 -2.390625 0.71875 -2.53125 0.6875 -2.53125 C 0.5625 -2.53125 0.359375 -2.328125 0.359375 -2.140625 L 0.359375 -0.15625 C 0.359375 0.015625 0.5625 0.21875 0.671875 0.21875 C 0.734375 0.21875 0.890625 0.15625 0.953125 0.046875 C 0.953125 0.046875 0.984375 0.015625 1.203125 -0.328125 C 1.34375 -0.171875 2.125 0.21875 3.015625 0.21875 C 4.171875 0.21875 5.171875 -0.890625 5.171875 -1.984375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.53125 -0.125 L 5.53125 -0.578125 C 4.640625 -0.578125 4.75 -0.515625 4.75 -1 L 4.75 -4.6875 L 2.890625 -4.5625 L 2.890625 -3.984375 C 3.78125 -3.984375 3.671875 -4.046875 3.671875 -3.5625 L 3.671875 -1.78125 C 3.671875 -0.921875 3.390625 -0.375 2.65625 -0.375 C 1.828125 -0.375 1.984375 -0.703125 1.984375 -1.21875 L 1.984375 -4.6875 L 0.125 -4.5625 L 0.125 -3.984375 C 1.09375 -3.984375 0.890625 -4.09375 0.890625 -3.203125 L 0.890625 -1.703125 C 0.890625 -0.921875 1.09375 0.109375 2.609375 0.109375 C 3.171875 0.109375 3.8125 -0.296875 4.09375 -0.921875 L 3.703125 -0.921875 L 3.703125 0.125 L 5.53125 -0.015625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-3\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.03125 -1.5625 4 -1.5625 C 3.921875 -1.5625 3.703125 -1.453125 3.671875 -1.390625 C 3.390625 -0.453125 2.9375 -0.40625 2.578125 -0.40625 C 2.046875 -0.40625 1.359375 -0.703125 1.359375 -2.296875 C 1.359375 -3.921875 1.984375 -4.21875 2.515625 -4.21875 C 2.609375 -4.21875 3.09375 -4.25 3.234375 -4.09375 C 3.171875 -4.09375 2.90625 -3.65625 2.90625 -3.515625 C 2.90625 -3.265625 3.296875 -2.9375 3.5625 -2.9375 C 3.828125 -2.9375 4.234375 -3.234375 4.234375 -3.53125 C 4.234375 -4.203125 3.265625 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.140625 -3.515625 0.140625 -2.28125 C 0.140625 -1 1.328125 0.109375 2.484375 0.109375 C 3.8125 0.109375 4.34375 -1.21875 4.34375 -1.3125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-4\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.34375 -1.3125 C 4.34375 -1.421875 4.0625 -1.578125 4 -1.578125 C 3.921875 -1.578125 3.703125 -1.453125 3.671875 -1.359375 C 3.328125 -0.34375 2.625 -0.40625 2.53125 -0.40625 C 2.03125 -0.40625 1.78125 -0.671875 1.546875 -1.03125 C 1.25 -1.515625 1.3125 -2.078125 1.3125 -2.296875 L 3.890625 -2.296875 C 4.109375 -2.296875 4.34375 -2.4375 4.34375 -2.640625 C 4.34375 -3.625 3.59375 -4.734375 2.359375 -4.734375 C 1.203125 -4.734375 0.078125 -3.5625 0.078125 -2.328125 C 0.078125 -0.984375 1.328125 0.109375 2.46875 0.109375 C 3.6875 0.109375 4.34375 -1.125 4.34375 -1.3125 Z M 3.484375 -2.78125 L 1.3125 -2.78125 C 1.375 -4.125 2.015625 -4.25 2.359375 -4.25 C 3.375 -4.25 3.28125 -3.03125 3.28125 -2.78125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-5\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.78125 -1.40625 C 3.78125 -1.9375 3.421875 -2.328125 3.3125 -2.453125 C 2.984375 -2.765625 2.453125 -2.890625 2.03125 -2.96875 C 1.46875 -3.078125 1 -3.078125 1 -3.65625 C 1 -4 1.0625 -4.28125 1.921875 -4.28125 C 3.015625 -4.28125 2.875 -3.515625 2.890625 -3.203125 C 2.90625 -3.109375 3.34375 -3.015625 3.34375 -3.015625 L 3.203125 -2.984375 C 3.34375 -2.984375 3.53125 -3.15625 3.53125 -3.34375 L 3.53125 -4.359375 C 3.53125 -4.53125 3.34375 -4.734375 3.234375 -4.734375 C 3.1875 -4.734375 3.015625 -4.6875 2.890625 -4.578125 C 2.859375 -4.53125 2.765625 -4.453125 2.84375 -4.515625 C 2.625 -4.6875 2.078125 -4.734375 1.921875 -4.734375 C 0.703125 -4.734375 0.125 -3.921875 0.125 -3.375 C 0.125 -3.015625 0.34375 -2.65625 0.625 -2.4375 C 0.9375 -2.171875 1.359375 -2.078125 2.078125 -1.9375 C 2.296875 -1.890625 2.90625 -1.859375 2.90625 -1.140625 C 2.90625 -0.640625 2.765625 -0.375 1.984375 -0.375 C 1.140625 -0.375 0.984375 -0.8125 0.796875 -1.65625 C 0.765625 -1.78125 0.5625 -1.96875 0.453125 -1.96875 C 0.328125 -1.96875 0.125 -1.75 0.125 -1.578125 L 0.125 -0.265625 C 0.125 -0.09375 0.328125 0.109375 0.4375 0.109375 C 0.484375 0.109375 0.640625 0.0625 0.828125 -0.125 C 0.84375 -0.15625 0.84375 -0.171875 0.890625 -0.21875 C 1.1875 0.0625 1.78125 0.109375 1.984375 0.109375 C 3.125 0.109375 3.78125 -0.6875 3.78125 -1.40625 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-6\">\n",
       "<path style=\"stroke:none;\" d=\"M 6.28125 -4.671875 L 5.984375 -7.046875 L 0.125 -7.046875 L 0.125 -6.46875 L 0.5625 -6.46875 C 1.328125 -6.46875 1.15625 -6.484375 1.15625 -6.125 L 1.15625 -0.90625 C 1.15625 -0.546875 1.328125 -0.578125 0.5625 -0.578125 L 0.125 -0.578125 L 0.125 0.015625 C 0.671875 -0.03125 1.453125 -0.03125 1.84375 -0.03125 C 2.25 -0.03125 3.15625 -0.03125 3.71875 0.015625 L 3.71875 -0.578125 L 3.1875 -0.578125 C 2.25 -0.578125 2.4375 -0.5625 2.4375 -0.921875 L 2.4375 -3.234375 L 3.09375 -3.234375 C 4.0625 -3.234375 3.953125 -3.046875 3.953125 -2.078125 L 4.609375 -2.078125 L 4.609375 -4.984375 L 3.953125 -4.984375 C 3.953125 -4 4.0625 -3.8125 3.09375 -3.8125 L 2.4375 -3.8125 L 2.4375 -6.203125 C 2.4375 -6.53125 2.265625 -6.46875 2.734375 -6.46875 L 3.921875 -6.46875 C 5.421875 -6.46875 5.46875 -6.046875 5.640625 -4.53125 L 6.296875 -4.53125 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-7\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.890625 -2.265625 C 4.890625 -3.53125 3.703125 -4.734375 2.5 -4.734375 C 1.25 -4.734375 0.078125 -3.515625 0.078125 -2.265625 C 0.078125 -0.984375 1.3125 0.109375 2.484375 0.109375 C 3.6875 0.109375 4.890625 -1 4.890625 -2.265625 Z M 3.671875 -2.34375 C 3.671875 -1.984375 3.734375 -1.546875 3.515625 -1.109375 L 3.453125 -1.078125 C 3.234375 -0.625 2.984375 -0.40625 2.5 -0.40625 C 2.0625 -0.40625 1.765625 -0.578125 1.5 -1.03125 C 1.25 -1.46875 1.3125 -1.984375 1.3125 -2.34375 C 1.3125 -2.734375 1.25 -3.1875 1.484375 -3.625 C 1.75 -4.078125 2.078125 -4.25 2.484375 -4.25 C 2.921875 -4.25 3.203125 -4.0625 3.46875 -3.640625 C 3.734375 -3.203125 3.671875 -2.71875 3.671875 -2.34375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-8\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.828125 -3.921875 C 3.828125 -4.25 3.3125 -4.671875 2.890625 -4.671875 C 2.15625 -4.671875 1.609375 -3.9375 1.46875 -3.515625 L 1.859375 -3.4375 L 1.859375 -4.6875 L 0.078125 -4.546875 L 0.078125 -3.984375 C 0.984375 -3.984375 0.859375 -4.046875 0.859375 -3.5625 L 0.859375 -0.890625 C 0.859375 -0.4375 0.953125 -0.578125 0.078125 -0.578125 L 0.078125 0.015625 L 1.421875 -0.03125 C 1.8125 -0.03125 2.28125 -0.03125 2.875 0.015625 L 2.875 -0.578125 L 2.46875 -0.578125 C 1.734375 -0.578125 1.90625 -0.546875 1.90625 -0.90625 L 1.90625 -2.4375 C 1.90625 -3.421875 2.140625 -4.1875 2.890625 -4.1875 C 2.953125 -4.1875 2.84375 -4.234375 2.625 -4.328125 C 2.96875 -4.4375 2.578125 -4.171875 2.578125 -3.921875 C 2.578125 -3.640625 2.984375 -3.359375 3.203125 -3.359375 C 3.375 -3.359375 3.828125 -3.609375 3.828125 -3.921875 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-9\">\n",
       "<path style=\"stroke:none;\" d=\"M 5.015625 -1.015625 L 5.015625 -1.71875 L 4.359375 -1.71875 L 4.359375 -1.015625 C 4.359375 -0.4375 4.3125 -0.515625 4.203125 -0.515625 C 3.875 -0.515625 4.03125 -0.828125 4.03125 -0.875 L 4.03125 -2.875 C 4.03125 -3.296875 3.984375 -3.78125 3.625 -4.140625 C 3.234375 -4.53125 2.59375 -4.734375 2.109375 -4.734375 C 1.296875 -4.734375 0.40625 -4.125 0.40625 -3.46875 C 0.40625 -3.171875 0.8125 -2.875 1.0625 -2.875 C 1.34375 -2.875 1.71875 -3.203125 1.71875 -3.453125 C 1.71875 -3.578125 1.46875 -4.046875 1.328125 -4.046875 C 1.421875 -4.171875 1.78125 -4.25 2.09375 -4.25 C 2.578125 -4.25 2.953125 -3.984375 2.953125 -3.09375 L 2.953125 -2.859375 C 2.640625 -2.84375 1.9375 -2.8125 1.3125 -2.515625 C 0.5625 -2.171875 0.125 -1.515625 0.125 -1.078125 C 0.125 -0.265625 1.28125 0.109375 1.90625 0.109375 C 2.578125 0.109375 3.234375 -0.421875 3.421875 -0.890625 L 3.015625 -0.890625 C 3.0625 -0.484375 3.53125 0.0625 4 0.0625 C 4.203125 0.0625 5.015625 -0.203125 5.015625 -1.015625 Z M 2.953125 -1.53125 C 2.953125 -0.578125 2.4375 -0.375 1.984375 -0.375 C 1.5 -0.375 1.28125 -0.59375 1.28125 -1.09375 C 1.28125 -1.640625 1.5 -2.328125 2.953125 -2.375 Z \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1503807966066013-0-10\">\n",
       "<path style=\"stroke:none;\" d=\"M 3.515625 -1.359375 L 3.515625 -2.078125 L 2.859375 -2.078125 L 2.859375 -1.390625 C 2.859375 -0.640625 2.765625 -0.40625 2.390625 -0.40625 C 1.71875 -0.40625 1.921875 -1.171875 1.921875 -1.34375 L 1.921875 -3.984375 L 3.34375 -3.984375 L 3.34375 -4.5625 L 1.921875 -4.5625 L 1.921875 -6.40625 L 1.28125 -6.40625 C 1.265625 -5.515625 1.171875 -4.515625 -0.015625 -4.46875 L -0.015625 -3.984375 L 0.84375 -3.984375 L 0.84375 -1.359375 C 0.84375 -0.140625 1.96875 0.109375 2.328125 0.109375 C 3.03125 0.109375 3.515625 -0.734375 3.515625 -1.359375 Z \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 0.00103125 -7.140031 L 0.00103125 -20.925188 \" transform=\"matrix(1,0,0,-1,21.831,6.942)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196685 1.594781 C -1.095122 0.997125 -0.0013725 0.0986875 0.299409 0.00103125 C -0.0013725 -0.100531 -1.095122 -0.995063 -1.196685 -1.592719 \" transform=\"matrix(0,1,1,0,21.831,27.86856)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-1\" x=\"5.731\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-2\" x=\"11.270228\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-3\" x=\"16.809456\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-3\" x=\"21.232868\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-4\" x=\"25.65628\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-5\" x=\"30.079692\" y=\"10.344\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-5\" x=\"34.004972\" y=\"10.344\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-6\" x=\"3.321\" y=\"38.621\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-7\" x=\"9.826604\" y=\"38.621\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-8\" x=\"14.807924\" y=\"38.621\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-4\" x=\"18.713279\" y=\"38.621\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-3\" x=\"23.136691\" y=\"38.621\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-9\" x=\"27.560103\" y=\"38.621\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-5\" x=\"32.541423\" y=\"38.621\"/>\n",
       "  <use xlink:href=\"#glyph-1503807966066013-0-10\" x=\"36.466703\" y=\"38.621\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "BayesNets.BayesNet{BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}}({2, 1} directed simple Int64 graph, BayesNets.CPDs.CategoricalCPD{Distributions.Categorical{Float64}}[2 instantiations:\n",
       "  Success (2), 6 instantiations:\n",
       "  Forecast (3)\n",
       "  Success (2)], Dict(:Forecast=>2,:Success=>1))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bn = readxdsl(Pkg.dir(\"BayesNets\", \"test\", \"sample_bn.xdsl\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Bayesian Score for a Network Structure\n",
    "\n",
    "The bayesian score for a discrete-valued BayesNet can can be calculated based only on the structure and data (the CPDs do not need to be defined beforehand). This is implemented with a method of `bayesian_score` that takes in a directed graph, the names of the nodes and data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-29.642240688899513"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3], \n",
    "                 b=[1,1,1,2,2,2,2,1,1,2,1,1],\n",
    "                 a=[1,1,1,2,1,1,2,1,1,2,1,1])\n",
    "g = DAG(3)\n",
    "add_edge!(g,1,2); add_edge!(g,2,3); add_edge!(g,1,3)\n",
    "bayesian_score(g, [:a,:b,:c], data)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
